django - 我真的被困在这个循环中好几天了。编辑/更新无法检测到模型的 pk?我应该提交工单吗?
问题描述
错误 :
.Reverse for 'patient_update' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['patients/update/(?P<pk>[0-9]+)$']?
每当我尝试在模板中调用 url 时,无论是在表单标签的操作中还是在普通的锚点中。
我创建了一个视图,用户可以在其中编辑患者信息,并且当我使用自定义用户模型时,我必须进行自定义更新/删除视图。当我通过 url 手动输入视图但当我放置链接时,视图正在工作通过锚标记引用它们我收到一个错误,它无法访问 pk,我尝试在此处遵循许多答案和 YT 上的视频,但错误仍然存在
视图.py
def PatientUpdateView(request,pk=None):
patient = get_object_or_404(models.Patient, pk=pk)
form = forms.PatientForm(request.POST or None ,instance=patient)
if form.is_valid() :
patient = form.save(commit=False)
patient.save()
messages.success(request,"patient updated!")
context = {
'patient': patient,
'form': form
}
return render(request,'patients/patient_edit_form_success.html',context)
else:
context = {
'patient' : patient,
'form': form,
}
return render(request, 'patients/patient_edit_form.html', context)
这就是我在锚标签中的称呼
<a class="btn btn-info btn-sm" href="{% url 'patients:patient_update' patient.pk %}">Open</a>
我试过了
pk = patient.pk
pk = pk
pk={{ patient.pk}}
以及许多其他的调用方式。
urls.py 这是我使用的 url 模式
path('update/<int:pk>', views.PatientUpdateView, name='patient_update'),
我尝试在 url 模式中使用 url 方法
我什至尝试使用基于类的视图,所以您知道我该如何解决吗?
更多信息
表格.py
class PatientTable(tables.Table):
FirstName = tables.Column(linkify=("patients:patient_detail", {"pk": tables.A("pk")}))
LastName = tables.Column(linkify=("patients:patient_detail", {"pk": tables.A("pk")}))
Telephone_no = tables.Column(linkify=("patients:patient_detail", {"pk": tables.A("pk")}))
# delete = tables.LinkColumn('patients:patient_delete', args=[A('pk')], attrs={
# 'a': {'class': 'btn'}
# })
# update = tables.LinkColumn('patients:patient_update', args=[A('pk')], attrs={
# 'a': {'class': 'btn'}
# })
edit = TemplateColumn(template_name='patients/edit_btn.html')
# delete = TemplateColumn(template_name='patients/patient_delete.html')
class Meta:
model = Patient
attrs = {'class': 'table table-striped table-hover'}
exclude = ("user", "Notes", "Adress")
template_name = 'django_tables2/bootstrap4.html'
和列表视图
def Patients_list(request):
patients = Patient
table = PatientTable(patients.objects.filter(user=request.user))
RequestConfig(request).configure(table)
return render(request, 'patients/patients_list.html',{
'table' : table
})
并在列表视图的 html 文件中:
{% extends 'base.html' %}
{% load render_table from django_tables2 %}
{% block content %}
<style>
#content{
margin-left : 18%;
margin-right : 2%;
}
</style>
<div id="content">
{% if user.is_authenticated %}
<h1> Patients list: </h1>
<br>
<a href="{%url 'patients:patient_create'%}" class="btn btn-info" role="button">Add Patient</a>
<br>
<br>
{% render_table table %}
{% else %}
<h2>please login</h2>
{% endif %}
{% endblock %}
</div>
患者 edit_form.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
#content{
margin-top: 100px;
margin-left : 18%;
margin-right : 2%;
}
{% block content %}
<div id="content">
<h2>Add Patient</h2>
<form method="POST" >
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary" >Update</button>
<p></p>
</form>
</div>
{% endblock %}
解决方案
我在我的 tables.py 中使用了它并且它有效:
edit = tables.LinkColumn('patients:patient_update',text='Edit', args=[A('pk')], attrs={
'a': {'class': 'btn btn-primary'}
})
推荐阅读
- android - Android Studio 中的 setOnClickListener 错误
- django - 如何在更新视图中在 django 中进行可清除的文件输入
- openfaas - Openfaas 使用队列名称调用异步函数
- oracle - 当我尝试更新数据时 ID 为 NULL
- java - Python生成的zlib + base64文本在Java中得到不正确的标头检查异常
- ios - SwiftUI:如何从 DatePicker 和 Date 中获取 Just time 值(Int)?
- angular - 重新加载 Angular SSR 应用程序时出现 504 网关错误
- asynchronous - serial_port::async_read_some 在没有睡眠的情况下无法工作
- java - Flink 的 QueryableStateClient 抛出 'FlinkRuntimeException: com.esotericsoftware.kryo.KryoException: Encountered unregistered class ID: 85'
- react-native - 如何在本机反应中从一个屏幕导航到另一个屏幕?