我正在嘗試創建一個存盤問題(特別是數學問題)并將其顯示給用戶的應用程式。我還沒有添加很多我想要的功能,因為我對 Django 還很陌生,所以我一直在關注 Django 教程專案,并根據我的需要進行更改。但是,即使我似乎傳入了正確的引數,我也遇到了 NoReverseMatch 錯誤。我的代碼如下。
模型.py
import imp
from django.db import models
from django.urls import reverse
import uuid
# Create your models here.
class Source(models.Model):
'''Model that represents the source of a problem (e.g. AMC, AIME, etc.)'''
problem_source = models.CharField(max_length=20)
problem_number = models.PositiveSmallIntegerField()
def __str__(self):
'''toString() method'''
return f'{self.problem_source} #{self.problem_number}'
class Topic(models.Model):
'''Model that represents the topic of a problem (e.g. Intermediate Combo)'''
problem_topic = models.CharField(max_length=50)
problem_level = models.CharField(max_length=15)
def __str__(self):
return f'{self.problem_level} {self.problem_topic}'
class Problem(models.Model):
'''Model that represents each problem (e.g. AIME #1, AMC #11, etc.)'''
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
source = models.OneToOneField(Source, on_delete=models.RESTRICT, null=True, unique=True)
problem_text = models.TextField()
topic = models.ManyToManyField(Topic)
def __str__(self):
"""String for representing the Model object."""
return f'{self.source}'
def get_absolute_url(self):
"""Returns the url to access a detail record for this book."""
return reverse('problem-detail', args=[str(self.id)])
網址.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('problems/', views.ProblemListView.as_view(), name='problems'),
path('problem/<int:pk>', views.ProblemListView.as_view(), name='problem-detail'),
]
視圖.py
import imp
from django.shortcuts import render
# Create your views here.
from .models import Problem, Source, Topic
def index(request):
'''View function for home page of site'''
# generate counts of the main objects
num_problems = Problem.objects.all().count()
context = {
'num_problems': num_problems,
}
return render(request, 'index.html', context=context)
from django.views import generic
class ProblemListView(generic.ListView):
model = Problem
class ProblemDetailView(generic.DetailView):
model = Problem
我的 HTML 檔案的鏈接如下:
base_generic.html:鏈接 問題_list.html:鏈接問題
_detail.html
:鏈接
我的作業區結構如下:
trivial
catalog
migrations
static/css
styles.css
templates
catalog
problem_detail.html
problem_list.html
base_generic.html
index.html
__init.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
trivial
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
db.sqlite3
manage.py
我已經閱讀了其他 StackOverflow 帖子,但似乎沒有一個適用于我的情況。此外,在 problem_list.html 中,如果href鏈接中的值為Problem.get_absolute_url,則站點將加載,但單擊“所有問題”的鏈接將回傳同一頁面。但是,如果我prob.get_absolute_url輸入href鏈接,我會收到 NoReverseMatch 錯誤。
這是我得到的確切錯誤:
NoReverseMatch at /catalog/problems/
Reverse for 'problem-detail' with arguments '('41b936f7-3c08-4fb9-a090-2d466348d34d',)' not found. 1 pattern(s) tried: ['catalog/problem/(?P<pk>[0-9] )\\Z']
Request Method: GET
Request URL: http://127.0.0.1:8000/catalog/problems/
Django Version: 4.0.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'problem-detail' with arguments '('41b936f7-3c08-4fb9-a090-2d466348d34d',)' not found. 1 pattern(s) tried: ['catalog/problem/(?P<pk>[0-9] )\\Z']
Django告訴我錯誤源于prob.get_absolute_url呼叫problem_list.html
uj5u.com熱心網友回復:
問題是您id在Problem模型上是 a UUID,但您的 URL 模式需要一個整數值作為pk- 因為您在命名模式前面加上了int::
path('problem/<int:pk>', views.ProblemListView.as_view(), name='problem-detail'),
如果您將其更改為:
path('problem/<uuid:pk>', views.ProblemListView.as_view(), name='problem-detail'),
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/432566.html
標籤:Python django django-url-reverse
下一篇:關系“”在Django中不存在
