I'm working on a project but I'm kind of stuck on a problem. My Django post form doesn't have any bug but every time I submit a form, it redirects as it should but doesn't display anything. And I have 5 forms of the same type but it's only one of them that does it.
Code Snippet Below.
views.py:
########################## PRESCRIPTION #####################################################
def patients_list(request):
context = {'patients_list': Prescription.objects.all()}
return render(request, 'dashboard/patients_list.html', context)
def patients_form(request, id=0):
if request.method == 'GET':
if id == 0:
pform = PatientsForm()
else:
prescription = Prescription.objects.get(pk=id)
pform = PatientsForm(instance=prescription)
return render(request, 'dashboard/patients_form.html', {'pform': pform})
else:
if id == 0:
pform = PatientsForm(request.POST)
else:
prescription = Prescription.objects.get(pk=id)
pform = PatientsForm(request.POST, instance=prescription)
if pform.is_valid():
pform.save()
return redirect('/list')
urls.py:
########################## PRESCRIPTION #####################################################
path('form', views.patients_form, name='patients_form'),
path('list', views.patients_list, name='patients_list'),
path('update_patient/<str:id>/', views.patients_form, name="update_patient"),
path('patients_delete/<str:id>/', views.patients_delete, name="patients_delete"),
########################## END PRESCRIPTION #####################################################
patients_form.html:
<form action="" method="POST">
{% csrf_token %}
<div >
{{pform.first_name|as_crispy_field}}
</div>
<div >
{{pform.last_name|as_crispy_field}}
</div>
<div >
{{pform.CNI|as_crispy_field}}
</div>
<div >
{{pform.gender|as_crispy_field}}
</div>
<div >
{{pform.marital_status|as_crispy_field}}
</div>
<div >
{{pform.telephone1|as_crispy_field}}
</div>
<div >
{{pform.telephone2|as_crispy_field}}
</div>
<div >
{{pform.town|as_crispy_field}}
</div>
<div >
{{pform.address|as_crispy_field}}
</div>
<div >
{{pform.occupation|as_crispy_field}}
</div>
<div >
{{pform.status|as_crispy_field}}
</div>
<div >
<div >
<button type="submit"> <i > </i> Submit</button>
</div>
<div >
<a href="{% url 'patients_list' %}" >Back To List
<i ></i>
</a>
</div>
</div>
</form>
forms.py:
class PatientsForm(forms.ModelForm):
class Meta:
model = Prescription
fields = '__all__'
labels = {
'first_name': 'First Name',
'last_name': 'Last Name'
}
patients_list.html:
{% for prescription in patients_list %}
<tbody>
<tr>
<td>
<div >
<input
type="checkbox"
id="customCheckBox2"
required=""
/>
<label
for="customCheckBox2"
></label>
</div>
</td>
<td>{{prescription.id}}</td>
<td>{{prescription.date_added}}</td>
<td>{{prescription.first_name}}</td>
<td>{{prescription.last_name}}</td>
<td>{{prescription.age}}Years</td>
<td>{{prescription.doctor}}</td>
<td>{{prescription.town}}</td>
<td>{{prescription.gender}}</td>
<td>
{% if prescription.status == 'New Patient' %}
<span >
<i ></i>
{{prescription.status}}
</span>
{% elif prescription.status == 'In Treatement' %}
<span >
<i ></i>
{{prescription.status}}
</span>
{% elif prescription.status == 'Recovered' %}
<span >
<i ></i>
{{prescription.status}}
</span>
{% endif %}
</td>
<td>
<a href="{% url 'update_patient' prescription.id %}" class='btn text-secondary px-0'>
<i ></i> Edit
</a>
</td>
<td>
<form action="{% url 'patients_delete' prescription.id %}" method='post' class='d-inline'>
{% csrf_token %}
<button type="submit"><i ></i> Delete
</button>
</form>
</td>
</tr>
</tbody>
{% endfor %}
CodePudding user response:
please, try to use DGCBV in your case createView
and updateView
. It can be much much better. more here: https://docs.djangoproject.com/en/4.1/ref/class-based-views/flattened-index/#editing-views
in your case:
def patients_form(request, id=0):
if request.method == 'GET':
# some staff on get without return
else:
# some staffon post
if pform.is_valid():
pform.save()
return redirect('/list')
return render(request, 'dashboard/patients_form.html', {'pform': pform})
in this code you return form-render if form is NOT valid. Otherwise instance should be saved and you goes to '/list'