我正在尝试在Django中创建 CRUD 项目。我成功连接MS SQL SERVER中的数据库,也成功地使用我的模型进行迁移。现在,当我尝试在网页中显示数据列表时,我接下来错误:
NoReverseMatch
使用参数反转'extraction_edit' '('A4A49247-B940-450C-8E1E-2F8148CB0933',)'和关键字参数'{}' 未找到。尝试了1种模式:
['crud/edit/(?P<pk>\\d+)$']
并在我的模板中显示错误:
<a href="{% url 'crud:extraction_edit' extraction.id_extraction %}">edit</a>
<a href="{% url 'crud:extraction_delete' extraction.id_extraction %}">delete</a>
有人可以帮我解决这个问题吗?!
urls.py
urlpatterns = (
url(r'^edit/(?P<pk>\d+)$', views.ExtractionUpdate.as_view(), name='extraction_edit'),
url(r'^delete/(?P<pk>\d+)$', views.ExtractionDelete.as_view(), name='extraction_delete'),
)
extraction_list.html
<h1>Extractions list</h1>
<ul>
{% for extraction in object_list %}
<li>ExtractionID-{{ extraction.id_extraction }} Date-{{ extraction.name_extraction }}
<a href="{% url 'crud:extraction_edit' extraction.id_extraction %}">edit</a>
<a href="{% url 'crud:extraction_delete' extraction.id_extraction %}">delete</a>
</li>
{% endfor %}
</ul>
<a href="{% url 'crud:extraction_new' %}">New</a>
models.py
class Extraction(models.Model):
id_extraction = models.CharField(db_column='ID_Extraction', max_length=36, primary_key=True)
name_extraction = models.CharField(db_column='Name_Extraction', max_length=150, blank=True, null=True)
class Meta:
managed = False
db_table = 'Extraction'
EDIT
:
model.py
def get_edit_url(self):
return reverse('extraction_edit', kwargs={'pk': self.id_extraction})
urls.py
<a href="{% url 'crud:extraction_edit' extraction.get_edit_url %}">EDIT</a>
答案 0 :(得分:1)
在你的urlpattern extract_edit中你有一个关键字参数pk。但是您没有在模板中指定它。 而不是
<a href="{% url 'crud:extraction_edit' extraction.id_extraction %}">edit</a>
像这样重写href属性:
<a href="{% url 'crud:extraction_edit' pk=extraction.id_extraction %}">edit</a>
引自Django doc:
关键字参数由正则表达式匹配的任何命名组组成,由django.conf.urls.url()的可选kwargs参数中指定的任何参数覆盖。