SUIIIII
SUIIIII

Reputation: 27

Django - Taking values from POST

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

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

<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.

Upvotes: 0

Views: 54

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599450

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 %}.

Upvotes: 0

Dipen Dadhaniya
Dipen Dadhaniya

Reputation: 4630

Use the URL template tag.

Change:

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

To:

<form action="{% url 'arrived' p.id %}" method="post">

Upvotes: 1

Related Questions