0

I'm passing some data to my template ("appointments.html") which looks like this:

Appointments today

Appointment ID : 84218332 Scheduled Time: 2019-10-18T01:00:00

  • Arrived

Appointment ID : 84218332 Scheduled Time: 2019-10-18T22:05:00

  • Arrived
<h1>Appointments today</h1>
    {% for p in appointment %}
        <tr>
            <td>Appointment ID : {{ p.patient }} Scheduled Time: {{p.scheduled_time}}</td>
            <td>
            <form action="{{ p.id }}" method="post">
              {% csrf_token %}
              <input type="hidden" name="appid" value="{{ p.id }}">
              <input type="submit" value="Arrived" class="btn btn-primary">
            </form>
            </td>

          </tr>
    {% endfor %}

I want to call another view in views.py by clicking on "Arrived" button which gets back the p.id which is passed as a value to further use it for other purposes.

urls.py :

url(r'^appointment/<int:appid>/$', views.arrived, name='arrived')

views.py

def arrived(request, appid):
        if request.method == 'POST':

            print(appid)

ERROR :

Using the URLconf defined in drchrono.urls, Django tried these URL patterns, in this order:

^setup/$ [name='setup']
^welcome/$ [name='welcome']
^appointment/$ [name='appointment']
^appointment/<int:appid>/$ [name='arrived']
^schedule/$ [name='schedule']
^patient_checkin/$ [name='checkin']
^update_info/$ [name='update']
^admin/
^login/(?P<backend>[^/]+)/$ [name='begin']
^complete/(?P<backend>[^/]+)/$ [name='complete']
^disconnect/(?P<backend>[^/]+)/$ [name='disconnect']
^disconnect/(?P<backend>[^/]+)/(?P<association_id>\d+)/$ [name='disconnect_individual']
The current path, appointment/131848814, didn't match any of these.

How do I fix this and what am I missing exactly?

EDIT: Changed my approach. Thought this is easier.

SUIIIII
  • 27
  • 1
  • 6
  • just a guess, but `
    ` -> `
    `, or even `accounts/arrived/`.
    – monkut Oct 20 '19 at 02:49
  • I had tried that, it doesnt work. @monkut – SUIIIII Oct 20 '19 at 02:54
  • Show more of your error, I suspect this is your console output of the dev server, show the full traceback including the call. The problem appears to be that your not hitting your `arrived` view. – monkut Oct 20 '19 at 02:59
  • @monkut please check the edit, this is simpler and I guess I am missing something simple now. – SUIIIII Oct 20 '19 at 03:07
  • I suggest you write a testcase to cover this. This may help: https://stackoverflow.com/questions/7304248/how-should-i-write-tests-for-forms-in-django – monkut Oct 20 '19 at 04:40

2 Answers2

1

Use the URL template tag.

Change:

<form action="{{ p.id }}" method="post">

To:

<form action="{% url 'arrived' p.id %}" method="post">
Dipen Dadhaniya
  • 4,550
  • 2
  • 16
  • 24
0

You're mixing up the old url and the new path syntaxes. Your URL should either be:

path('appointment/<int:appid>/', views.arrived, name='arrived')

or

url(r'^appointment/(?P<appid>\d+)/$', views.arrived, name='arrived')

Also, as Dipen noted in their answer, you should change the form action to {% url 'arrived' p.id %}.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I'm accepting this answer as accepted. Although, @Dipen Dadhaniya your suggestion was important too. – SUIIIII Oct 21 '19 at 00:08