如何在 Django 中向 url 添加 slug 参数?
how to add slug argument to url in django?
我想像这样使用 django 在 url 中添加 slug <a href="{% url 'tutorials:tutorial' topic.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %} </a>
我真的不知道如何在 url 中传递三重 slug 例如:我想访问编程 > html > 介绍-html 像这样 http://127.0.0.1:8000/tutorial/programming/html/introduction-to-html
错误
Reverse for 'tutorial' with arguments '('', 'html', 'introduction-to-html')' not found. 1 pattern(s) tried: ['tutorial/(?P<main_category_slug>[^/]+)/(?P<topic_category_slug>[^/]+)/(?P<tutorial_slug>[^/]+)$']
topic.html:
{% for topic in topics %}
<a href="{% url 'tutorials:tutorial' topic.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %}">{{topic.title}} - Start Now</a>
{% endfor %}
views.py:
def topic(request, main_category_slug, topic_category_slug):
tutorial_category = TutorialCategory.objects.get(slug=main_category_slug)
tutorial_topic_category = TutorialTopicCategory.objects.get(slug=topic_category_slug)
topics = Topic.objects.filter(tutorial_topic_category=tutorial_topic_category)
context = {
'topics':topics,
}
return render(request, 'tutorials/topic.html', context)
def tutorial(request, main_category_slug, topic_category_slug, tutorial_slug):
tutorial_category = TutorialCategory.objects.get(slug=main_category_slug)
tutorial_topic_category = TutorialTopicCategory.objects.get(slug=topic_category_slug)
topics = Topic.objects.filter(tutorial_topic_category=tutorial_topic_category)
tutorial = Topic.objects.get(slug=tutorial_slug)
context = {
'topics':topics,
'tutorial':tutorial,
}
return render(request, 'tutorials/tutorial.html', context)
urls.py
path("<slug>", views.tutorial_topic_category, name='tutorial-topic-category'),
path("<slug:main_category_slug>/<slug:topic_category_slug>", views.topic, name='topic'),
path("<slug:main_category_slug>/<slug:topic_category_slug>/<slug:tutorial_slug>/", views.tutorial, name='tutorial'),
models.py
class TutorialCategory(models.Model):
title = models.CharField(max_length=1000)
slug = models.SlugField(unique=True)
active = models.BooleanField(default=True)
class TutorialTopicCategory(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
tutorial_category = models.ForeignKey(TutorialCategory, on_delete=models.CASCADE, null=True)
title = models.CharField(max_length=1000)
slug = models.SlugField(unique=True)
class Topic(models.Model):
tutorial_topic_category = models.ForeignKey(TutorialTopicCategory, on_delete=models.CASCADE, null=True)
title = models.CharField(max_length=10000)
slug = models.SlugField(unique=True)
默认情况下,如果您在 path() 中定义时没有在路由中提供任何内容,就像您提供 "<main_category_slug>/<topic_category_slug>/<tutorial_slug>"
那样,这些被视为字符串。
例如:<str:name>/
和<name>/
是一样的。
如果你想把这三个都做成slug,那么你应该先把它们定义为slug,把你的路线写成"<slug:main_category_slug>/<slug:topic_category_slug>/<slug:tutorial_slug>/"
,然后这些就被认为是slug。
小错误:你不应该定义相同的变量,它必须是 for topic in topics
而不是 for topic in topic
.
Note:
Always give /
at the end of every route.
编辑:
您应该在代码中执行以下操作:
首先,改变这个link:
<a href="{% url 'tutorials:tutorial' topic.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %}">{{topic.title}} - Start Now</a>
对此link:
<a href="{% url 'tutorials:tutorial' topic.tutorial_topic_category.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %}">{{topic.title}} - Start Now</a>
为什么?
正确看待你的url:
path("<slug:main_category_slug>/<slug:topic_category_slug>/<slug:tutorial_slug>/", views.tutorial, name='tutorial')
它的第一个 slug 是 main_category_slug
,表示模板中的 slugof
TutorialCategorymodel which is at the first in <i>models.py</i>. So, for accessing you need to write
topic.tutorial_topic_category.tutorial_category.slug` tutorial.html.
它的第二个 slug 是 topic_category_slug
,表示 TutorialTopicCategory
模型的 slug
,在 models.py 中排在第二个。因此,要访问它,您需要在模板 tutorial.html.
中写入 topic.tutorial_topic_category.slug
它的第三个弹头是 tutorial_slug
这意味着你的 Topic
模型的 slug
在 models.py 中排在最后.因此,要访问它,您需要在模板 tutorial.html.
中简单地编写 topic.slug
只需更改此内容,不要更改任何内容:
topic.html 或模板文件:
{% for topic in topics %}
<a href="{% url 'tutorials:tutorial' topic.tutorial_topic_category.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %}">{{topic.title}} - Start Now</a>
{% endfor %}
我想像这样使用 django 在 url 中添加 slug <a href="{% url 'tutorials:tutorial' topic.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %} </a>
我真的不知道如何在 url 中传递三重 slug 例如:我想访问编程 > html > 介绍-html 像这样 http://127.0.0.1:8000/tutorial/programming/html/introduction-to-html
错误
Reverse for 'tutorial' with arguments '('', 'html', 'introduction-to-html')' not found. 1 pattern(s) tried: ['tutorial/(?P<main_category_slug>[^/]+)/(?P<topic_category_slug>[^/]+)/(?P<tutorial_slug>[^/]+)$']
topic.html:
{% for topic in topics %}
<a href="{% url 'tutorials:tutorial' topic.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %}">{{topic.title}} - Start Now</a>
{% endfor %}
views.py:
def topic(request, main_category_slug, topic_category_slug):
tutorial_category = TutorialCategory.objects.get(slug=main_category_slug)
tutorial_topic_category = TutorialTopicCategory.objects.get(slug=topic_category_slug)
topics = Topic.objects.filter(tutorial_topic_category=tutorial_topic_category)
context = {
'topics':topics,
}
return render(request, 'tutorials/topic.html', context)
def tutorial(request, main_category_slug, topic_category_slug, tutorial_slug):
tutorial_category = TutorialCategory.objects.get(slug=main_category_slug)
tutorial_topic_category = TutorialTopicCategory.objects.get(slug=topic_category_slug)
topics = Topic.objects.filter(tutorial_topic_category=tutorial_topic_category)
tutorial = Topic.objects.get(slug=tutorial_slug)
context = {
'topics':topics,
'tutorial':tutorial,
}
return render(request, 'tutorials/tutorial.html', context)
urls.py
path("<slug>", views.tutorial_topic_category, name='tutorial-topic-category'),
path("<slug:main_category_slug>/<slug:topic_category_slug>", views.topic, name='topic'),
path("<slug:main_category_slug>/<slug:topic_category_slug>/<slug:tutorial_slug>/", views.tutorial, name='tutorial'),
models.py
class TutorialCategory(models.Model):
title = models.CharField(max_length=1000)
slug = models.SlugField(unique=True)
active = models.BooleanField(default=True)
class TutorialTopicCategory(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
tutorial_category = models.ForeignKey(TutorialCategory, on_delete=models.CASCADE, null=True)
title = models.CharField(max_length=1000)
slug = models.SlugField(unique=True)
class Topic(models.Model):
tutorial_topic_category = models.ForeignKey(TutorialTopicCategory, on_delete=models.CASCADE, null=True)
title = models.CharField(max_length=10000)
slug = models.SlugField(unique=True)
默认情况下,如果您在 path() 中定义时没有在路由中提供任何内容,就像您提供 "<main_category_slug>/<topic_category_slug>/<tutorial_slug>"
那样,这些被视为字符串。
例如:<str:name>/
和<name>/
是一样的。
如果你想把这三个都做成slug,那么你应该先把它们定义为slug,把你的路线写成"<slug:main_category_slug>/<slug:topic_category_slug>/<slug:tutorial_slug>/"
,然后这些就被认为是slug。
小错误:你不应该定义相同的变量,它必须是 for topic in topics
而不是 .for topic in topic
Note:
Always give/
at the end of every route.
编辑:
您应该在代码中执行以下操作:
首先,改变这个link:
<a href="{% url 'tutorials:tutorial' topic.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %}">{{topic.title}} - Start Now</a>
对此link:
<a href="{% url 'tutorials:tutorial' topic.tutorial_topic_category.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %}">{{topic.title}} - Start Now</a>
为什么?
正确看待你的url:
path("<slug:main_category_slug>/<slug:topic_category_slug>/<slug:tutorial_slug>/", views.tutorial, name='tutorial')
它的第一个 slug 是 main_category_slug
,表示模板中的 slugof
TutorialCategorymodel which is at the first in <i>models.py</i>. So, for accessing you need to write
topic.tutorial_topic_category.tutorial_category.slug` tutorial.html.
它的第二个 slug 是 topic_category_slug
,表示 TutorialTopicCategory
模型的 slug
,在 models.py 中排在第二个。因此,要访问它,您需要在模板 tutorial.html.
topic.tutorial_topic_category.slug
它的第三个弹头是 tutorial_slug
这意味着你的 Topic
模型的 slug
在 models.py 中排在最后.因此,要访问它,您需要在模板 tutorial.html.
topic.slug
只需更改此内容,不要更改任何内容:
topic.html 或模板文件:
{% for topic in topics %}
<a href="{% url 'tutorials:tutorial' topic.tutorial_topic_category.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %}">{{topic.title}} - Start Now</a>
{% endfor %}