我很确定这是一个简单的错误,但我似乎无法弄明白。该错误表明找不到关键字参数,但我在html中有它们。
这是错误:
bsub
models.py
NoReverseMatch at /questions/questionupdate/user2-question3/4/
Reverse for 'detail' with keyword arguments '{'pk': 4}' not found. 1 pattern(s) tried: ['questions/questiondetail/(?P<slug>[\\w-]+)/(?P<pk>\\d+)/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/questions/questionupdate/user2-question3/4/
Django Version: 1.11
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'detail' with keyword arguments '{'pk': 4}' not found. 1 pattern(s) tried: ['questions/questiondetail/(?P<slug>[\\w-]+)/(?P<pk>\\d+)/$']
file structure:
project: UserTest
--->app:accounts
--->app:questions
urls.py 我正在尝试将slug和pk传递给url
class Question(models.Model):
class Meta:
ordering = ['-date_updated']
user = models.ForeignKey(User, related_name="question")
# completedTODO: get user working^
question = models.TextField(blank=False, null=False) # unique=True,
question_html = models.TextField(blank=False, null=False)
answer = models.TextField(blank=False, null=False)
answer_html = models.TextField(blank=False, null=False)
slug = models.SlugField(unique=True, default='')
tags = TaggableManager()
def __str__(self):
return self.question
def save(self, *args, **kwargs):
self.question_html = misaka.html(self.question)
self.answer_html = misaka.html(self.answer)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse(
"questions:detail",
kwargs={
# "slug": self.slug,
"pk": self.pk
}
)
views.py
url(r'questiondetail/(?P<slug>[\w-]+)/(?P<pk>\d+)/$', views.QuestionDetail.as_view(), name='detail'),
url(r'questionupdate/(?P<slug>[\w-]+)/(?P<pk>\d+)/$', views.QuestionUpdate.as_view(), name='update'),
question_detail.html
class QuestionDetail(generic.DetailView):
model = models.Question
class QuestionUpdate(generic.UpdateView):
model = models.Question
form_class = QuestionForm
template_name = "questions/question_form_update.html"
感谢任何帮助!
答案 0 :(得分:3)
似乎问题是由评论行造成的;)
def get_absolute_url(self):
return reverse(
"questions:detail",
kwargs={
# "slug": self.slug,
"pk": self.pk
}
)
取消注释,它应该有用。