Django反向错误

时间:2017-08-03 14:28:15

标签: python django

我正致力于此项目,列出患者详细信息并进行更新 获取错误未找到“update_patient”的反向。 'update_patient'不是有效的视图函数或模式名称。帮助修复错误

patient.html

  {% extends 'patient/base.html' %} {% block content %}
 <p>Patient: {{ patient }}</p>
  <p>Entries: </p>
  <p>Update Patient:</p>
  <p>
   <a href="{% url 'patient:new_entry' patient.id %}">add new entry</a>
  </p>
   <ul>
{% for entry in entries %}
<li>
    <p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
    <p>{{ entry.text|linebreaks }}</p>
    <p>
        <a href="{% url 'patient:edit_entry' entry.id %}">edit 
   entry</a>
    </p>
   </li>
   {% empty %}
   <li>
        There are no entries for this topic yet.
    </li>
    {% endfor %}
  </ul>

  <a href="{% url 'patient:update' patient.id %}">update</a>

   <a href="{% url 'patient:new_patient' %}">Add a new patient:</a> {% 
  endblock content %}

views.py

    def update_patient(request, patient_id):
patient = Patient.object.get(id=patient_id)
if request.method != 'POST':
    # Initial request; pre-fill form with the current entry.
    form = PatientForm(instance=patient)
else:
    # POST data submitted; process data.
    form = PatientForm(instance=patient, data=request.POST)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse('patient:patients',
                                    args=[patient.id]))
context = {'patient': patient, 'form': form}
return render(request, 'patient/update_patient.html', context)

urls.py

        url(r'^update_patient/(?P<patient_id>\d+)/$', views.update_patient, name='update'),

update_patient.html

      {% extends "patient/base.html" %} {% block content %}
  <p><a href="{% url 'patient:patient' patient.id %}">{{ patient }}</a>
     </p>
   <p>Update:</p>

     <form action="{% url 'patient:update_patient' patient.id %}" 
        method='post'>
      {% csrf_token %} {{ form.as_p }}
      <button name='submit'>update</button>
   </form>
     {% endblock content %}

0 个答案:

没有答案