[英]Django Form Post but doesn't display data
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.我的 Django 发布表单没有任何错误,但是每次我提交表单时,它都会按应有的方式重定向,但不显示任何内容。 And I have 5 forms of the same type but it's only one of them that does it.我有 5 个相同类型的 forms,但只有其中一个可以做到。
Code Snippet Below下面的代码片段
View.py视图.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网址.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患者表格.html
<form action="" method="POST">
{% csrf_token %}
<div class="form-group">
{{pform.first_name|as_crispy_field}}
</div>
<div class="form-group">
{{pform.last_name|as_crispy_field}}
</div>
<div class="form-group">
{{pform.CNI|as_crispy_field}}
</div>
<div class="form-group">
{{pform.gender|as_crispy_field}}
</div>
<div class="form-group">
{{pform.marital_status|as_crispy_field}}
</div>
<div class="form-group">
{{pform.telephone1|as_crispy_field}}
</div>
<div class="form-group">
{{pform.telephone2|as_crispy_field}}
</div>
<div class="form-group">
{{pform.town|as_crispy_field}}
</div>
<div class="form-group">
{{pform.address|as_crispy_field}}
</div>
<div class="form-group">
{{pform.occupation|as_crispy_field}}
</div>
<div class="form-group">
{{pform.status|as_crispy_field}}
</div>
<div class="row">
<div class="col md 6">
<button class="btn btn-success my-4" type="submit"> <i class="flaticon-381-database"> </i> Submit</button>
</div>
<div class="col md 6">
<a href="{% url 'patients_list' %}" class="btn btn-secondary btn-block">Back To List
<i class="fas fa-stream"></i>
</a>
</div>
</div>
</form>
Forms.py Forms.py
class PatientsForm(forms.ModelForm):
class Meta:
model = Prescription
fields = '__all__'
labels = {
'first_name': 'First Name',
'last_name': 'Last Name'
}
patients_list.html患者列表.html
{% for prescription in patients_list %}
<tbody>
<tr>
<td>
<div class="custom-control custom-checkbox">
<input
type="checkbox"
class="custom-control-input"
id="customCheckBox2"
required=""
/>
<label
class="custom-control-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 class="badge badge-outline-primary">
<i class="fa fa-circle text-primary mr-1"></i>
{{prescription.status}}
</span>
{% elif prescription.status == 'In Treatement' %}
<span class="badge badge-warning light">
<i class="fa fa-circle text-warning mr-1"></i>
{{prescription.status}}
</span>
{% elif prescription.status == 'Recovered' %}
<span class="badge badge-info light">
<i class="fa fa-circle text-info mr-1"></i>
{{prescription.status}}
</span>
{% endif %}
</td>
<td>
<a href="{% url 'update_patient' prescription.id %}" class='btn text-secondary px-0'>
<i class="fa fa-pencil fa-fw"></i> Edit
</a>
</td>
<td>
<form action="{% url 'patients_delete' prescription.id %}" method='post' class='d-inline'>
{% csrf_token %}
<button class="btn text-warning px-0" type="submit"><i class="fa fa-trash-o fa-fw"></i> Delete
</button>
</form>
</td>
</tr>
</tbody>
{% endfor %}
THANKS FOR THE HELP谢谢您的帮助
please, try to use DGCBV in your case createView
and updateView
.请尝试在您的情况下使用 DGCBV createView
和updateView
。 It can be much much better.它可以好得多。 more here: https://docs.djangoproject.com/en/4.1/ref/class-based-views/flattened-index/#editing-views更多信息: 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'
否则应保存实例,然后转到'/list'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.