首页 > 解决方案 > 我真的被困在这个循环中好几天了。编辑/更新无法检测到模型的 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 %}

标签: djangodjango-templatesdjango-views

解决方案


我在我的 tables.py 中使用了它并且它有效:

 edit = tables.LinkColumn('patients:patient_update',text='Edit', args=[A('pk')], attrs={
    'a': {'class': 'btn btn-primary'}
    })

推荐阅读