1
我正在處理的Django項目列出患者詳細信息並讓用戶編輯詳細信息。我已經能夠列出它,但views.py
沒有鏈接到更新列表的URL。Django編輯模型實例
views.py:
def update_patient(request, patient_id):
patient = Patient.objects.get(id=patient_id)
if request.method != 'POST':
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:patient',
args=[patient.id]))
context = { 'patient': patient, 'form': form}
return render(request, 'patient/update_patient.html', context)
models.py:
class Patient(models.Model):
patientID = models.CharField(max_length=20)
firstName =models.CharField(max_length=20)
lastName = models.CharField(max_length=20)
age = models.IntegerField(max_length=None)
SSN = models.CharField(max_length=15)
address = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
urls.py:
url(r'^patients/(?P<patient_id>\update\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 Patient:</p>
<form action="{% url 'patient:update' patient.id %}" method='post'>
{% csrf_token %} {{ form.as_p }}
<button name='submit'>add entry</button>
</form>
{% endblock content %}
試試這個URL(R」 ^患者/(?P [0-9] +)/ update $',views.update_patient,name ='update'), –
Vaibhav
謝謝。更正了網址我無法訪問瀏覽器中的更新詳細信息 – Kate
您正在收到什麼錯誤? – Vaibhav