![](/img/trans.png)
[英]django exception: Reverse for 'comment_new' with keyword arguments '{'pk': ''}' not found
[英]Django Reverse Match Error with keyword arguments '{'pk': ''}' not found
我是 Django 和一般編程的新手。 我正在閱讀《Django For Beginners》一書,並在 forms 部分添加了我自己的天賦,但遇到了問題。 下面我詳細說明了錯誤並提供了我的代碼。
ERROR NoReverseMatch at /drug/12/ Reverse for 'drug_edit' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['drug/(?P[0-9]+)/edit/$'] Request Method: GET Request URL: http://127.0.0.1:8000/drug/12/ Django Version : 3.1.3 異常類型:NoReverseMatch 異常值:
找不到關鍵字 arguments '{'pk': ''}' 的“drug_edit”。 嘗試了 1 種模式:['drug/(?P[0-9]+)/edit/$'] 異常位置:C:\Users\scott\django_sites\history_cloud1\history1_env\lib\site-packages\django \urls\resolvers.py, line 685, in _reverse_with_prefix Python Executable: C:\Users\scott\django_sites\history_cloud1\history1_env\Scripts\python.exe Python Version: 3.8.3
模型.py
from django.db import models
from django.urls import reverse
class DrugInfo(models.Model):
drug_name = models.CharField(max_length=75)
patient_name = models.CharField(max_length=25, default='Enter Patient Name',)
author = models.ForeignKey(
'auth.User',on_delete=models.CASCADE,
)
def __str__(self):
return self.drug_name
def get_absolute_url(self):
return reverse('drug_detail', args=[str(self.id)])
網址.py
from django.urls import path
from .views import (
drugListView,
drugDetailView,
drugCreateView,
drugDeleteView,
drugUpdateView,
)
urlpatterns = [
path('drug/<int:pk>/delete/',
drugDeleteView.as_view(), name='drug_delete'),
path('drug/new/', drugCreateView.as_view(), name='drug_new'),
path('drug/<int:pk>/', drugDetailView.as_view(), name='drug_detail'),
path('drug/<int:pk>/edit/',
drugUpdateView.as_view(), name='drug_edit'),
path('', drugListView.as_view(), name='home'),
]
視圖.py
from django.views.generic import ListView, DetailView
from django.views.generic.edit import (
CreateView, UpdateView, DeleteView
)
from django.urls import reverse_lazy
from .models import DrugInfo
class drugListView(ListView):
model = DrugInfo
template_name = 'home.html'
class drugDetailView(DetailView):
model = DrugInfo
template_name = 'drug_detail.html'
class drugCreateView(CreateView):
model = DrugInfo
template_name = 'drug_new.html'
fields = ['drug_name', 'patient_name', 'author']
class drugUpdateView(UpdateView): # new
model = DrugInfo
template_name = 'drug_edit.html'
fields = ['drug_name', 'patient_name']
class drugDeleteView(DeleteView): # new
model = DrugInfo
template_name = 'drug_delete.html'
success_url = reverse_lazy('home')
drug_edit.html
{% extends 'base.html' %}
{% block content %}
<h1>Edit Drug Information</h1>
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update">
</form>
{% endblock content %}
底座.html
{% load static %}
<html>
<head>
<title>Script Tracker Title Base HTML</title>
<link href="https://fonts.googleapis.com/css?family=Tangerine" rel="stylesheet">
<link href="{% static 'css/base.css' %}" rel="stylesheet">
</head>
<body>
<div>
<header>
<div class="nav-left">
<h1><a href="{% url 'home' %}">Script Tracker</a></h1>
</div>
<div class="nav-right">
<a href="{% url 'drug_new' %}">+ Add New Drug</a>
</div>
</header>
{% block content %}
{% endblock content %}
</div>
</body>
</html>
drug_detail.html
{% extends 'base.html' %}
{% block content %}
<div class="drug-entry">
<h2>{{ post.drug_name }}</h2>
<h2>{{ post.patient_name }}</h2>
</div>
<p><a href="{% url 'drug_edit' drug.pk %}">+ Edit Drug Information</a></p>
<p><a href="{% url 'drug_delete' drug.pk %}">+ Delete Drug</a></p>
{% endblock content %}
總結我對這一切都很陌生,所以如果我沒有發布所有需要的信息,我深表歉意。 我知道我已經很接近了,而且我對 pk 的東西有些搞砸了。 當我在瀏覽器中鍵入時, http://127.0.0.1:8000/drug/12/edit/工作正常。 任何建議將不勝感激。
在您的模板drug_detail.html
,您引用DrugInfo
object 如下: post
或drug
。 雖然實際上drugDetailView
從未將此類變量傳遞到上下文中。 默認情況下, DetailView
將實例傳遞為object
或<lowercase_model_name>
(此處為druginfo
)。 如果要設置它使用的名稱,可以在 class 上設置context_object_name
:
class drugDetailView(DetailView):
model = DrugInfo
template_name = 'drug_detail.html'
context_object_name = 'drug'
現在在模板drug_detail.html
:
{% extends 'base.html' %}
{% block content %}
<div class="drug-entry">
<!-- Use `drug` instead of `post` now -->
<h2>{{ drug.drug_name }}</h2>
<h2>{{ drug.patient_name }}</h2>
</div>
<p><a href="{% url 'drug_edit' drug.pk %}">+ Edit Drug Information</a></p>
<p><a href="{% url 'drug_delete' drug.pk %}">+ Delete Drug</a></p>
{% endblock content %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.