0

我将一些数据传递给我的模板(“appointments.html”),如下所示:

今天的约会

预约号:84218332 预约时间:2019-10-18T01:00:00

  • 到达的

预约号:84218332 预约时间: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 %}

我想通过单击“到达”按钮来调用views.py 中的另一个视图,该按钮取回作为值传递的p.id,以进一步将其用于其他目的。

urls.py:

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

视图.py

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

            print(appid)

错误 :

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.

我该如何解决这个问题,我到底错过了什么?

编辑:改变了我的方法。认为这更容易。

4

2 回答 2

1

使用URL 模板标记

改变:

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

到:

<form action="{% url 'arrived' p.id %}" method="post">
于 2019-10-20T04:26:29.383 回答
0

您正在混淆旧的 url 和新的路径语法。您的网址应该是:

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

或者

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

此外,正如 Dipen 在他们的回答中指出的那样,您应该将表单操作更改为{% url 'arrived' p.id %}.

于 2019-10-20T09:07:37.983 回答