0

Iam working on this project for listing patient details and updating them getting error Reverse for 'update_patient' not found. 'update_patient' is not a valid view function or pattern name. help to fix the error

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 %}
florence
  • 11
  • 5
  • Your `name='update'` in the URL pattern does not match `update_patient` in `{% url 'patient:update_patient' patient.id %}`. – Alasdair Aug 03 '17 at 14:30
  • thanks but now i get another error AttributeError at /update_patient/1/ type object 'Patient' has no attribute 'object' views.py in update_patient patient = Patient.object.get(id=patient_id) – florence Aug 03 '17 at 15:30
  • You have a typo -- it should be `Patient.objects` – Alasdair Aug 03 '17 at 15:31
  • sorry about that.. – florence Aug 03 '17 at 15:33
  • I am still getting the reverse error.. what am I missing – florence Aug 03 '17 at 15:38
  • views.py in update_patient return render(request, 'patient/update_patient.html', context) – florence Aug 03 '17 at 15:39
  • It sounds like you fixed the original error, then fixed the `AttributeError`, and now have a third error. We can't tell why that is because you haven't updated the code. The linked question *What is a NoReverseMatch error, and how do I fix it?* is a pretty comprehensive guide to solving `NoReverseMatch` errors. – Alasdair Aug 03 '17 at 15:49
  • sorry about that.. i updated the code: patient.html: update so when i click on update i get the reverse error.. – florence Aug 03 '17 at 16:12
  • `url(r'^update_patient/(?P\d+)/$', views.update_patient, name='update')` a reverse method looks for the name argument and matches accordingly. You named your view as update_patient but given a name of 'update'. So you need to change `name='update'` to `name='update_patient` to match with reverse method. – IVI Aug 03 '17 at 16:27
  • You had `{% url 'patient:update_patient' patient.id %}` twice in your original question. If you change it to `'patient:update'`, then you need to fix it in both places. Alternatively, you can change the name as @MuratSert says. The key thing is that they `{% url %}` tag and URL pattern name need to match *everywhere*. – Alasdair Aug 03 '17 at 16:29
  • Thank you so much .. the code is working perfectly... – florence Aug 03 '17 at 16:43

0 Answers0