我在sub-sub-category.html
文件中收到此错误。命名空间很好,URL名称似乎也很好,请看看,也许您可以帮我。
上面的错误可能是由于以下这一行,我在其中改写为那个sub_sub_sub_cat
网址:
{% for sub_cat in matching_series %}
<h5>{{ sub_cat.tutorial_title }}</h5>
<p>{{ sub_cat.snippet|safe }}</p>
<a class='card-link' href="{% url 'tutorial:sub_sub_sub_cat' sub_cat.tutorial_slug %}">
read more</a> # here
{% endfor %}
对于sub_cat.tutorial_slug
,我不太确定。无论如何,这就是教程模型:
class Tutorial(models.Model):
tutorial_title = models.CharField(max_length=150)
tutorial_content = models.TextField()
tutorial_published = models.DateTimeField(
"date Published", default=datetime.now())
tutorial_series = models.ForeignKey(
TutorialSeries, default=1, on_delete=models.SET_DEFAULT)
tutorial_slug = models.SlugField(default=1, blank=True)
class Section(models.Model):
section_title = models.CharField(max_length=150)
section_content = models.TextField()
section_published = models.DateTimeField(
"date Published", default=datetime.now())
section_tutorial = models.ForeignKey(
TutorialSeries, default=1, on_delete=models.SET_DEFAULT)
section_slug = models.SlugField(default=1, blank=True)
这是url
app_name = 'tutorial'
urlpatterns = [
path('', views.home_page, name='home'),
path('tutorial/<int:id>/', views.tutorial_detail, name='tutorial_detail'),
path('<single_slug>/', views.single_slug, name='single_slug'),
path('<sub_sub_cat>/done/', views.sub_sub_cat, name='sub_sub_cat'), #here
path('<sub_sub_sub_cat>/section/done/',
views.sub_sub_sub_cat, name='sub_sub_sub_cat)'),
]
和最后两个URL各自的视图在这里:
def sub_sub_cat(request, sub_sub_cat):
matching_series = Tutorial.objects.filter(
tutorial_series__series_slug=sub_sub_cat)
return render(request, 'tutorial/sub-sub-category.html', context={
"matching_series": matching_series,
})
def sub_sub_sub_cat(request, sub_sub_sub_cat):
matching_series = Section.objects.filter(
section_tutorial__tutorial_slug=sub_sub_sub_cat)
return render(request, 'tutorial/sub-sub-sub-cat.html', context={
"matching_series": matching_series,
})
感谢您的时间和帮助