如何将slug参数添加到django中的URL?
我想使用django在url中添加slug,例如this < a href =“ {%url'tutorials:tutorial'topor'topor.tutorial_category.slug topic.tutorial_topic_category.category.slug topic.slug topic.slug topic.slug%}代码>我真的不知道如何在URL中传递三重SLUG:我想访问编程> html> 127.0.0.1:8000/tutorial/programml/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.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)
I want to add slug in url using django like this <a href="{% url 'tutorials:tutorial' topic.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %} </a>
i dont really know how to pass in triple slug in the url for example: i want to access the programming > html > introduction-to-html like this http://127.0.0.1:8000/tutorial/programming/html/introduction-to-html
Error
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)
]
topic.html:
views.py:
urls.py
models.py
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,如果您在path()中定义
“&lt; main_category_slug&gt;/&lt;
例如:
&lt; str:名称&gt;/
and&lt; name&gt;/
是相同的。如果要使所有三个中的slugs都要使sl,则应该将它们定义为slug,将路由写为
“&lt; slug:main_category_slug&gt;/&gt;/&gt;/&lt; /code>,然后将其视为sl。
小错误:您不应定义相同的变量,对于主题中的主题> 而不是
,主题中的主题 。编辑:
您应该在代码中进行以下操作:
首先,将此链接更改为:
转到此链接:
为什么?
正确查看您的URL:
这是第一个slug是
main_category_slug
,它意味着tutorialCategory的slug
模型,该模型在 models.py.py.py.py 中。因此,要访问您需要编写topic.tutorial_topic_category.tutorial_category.slug
在模板 tutorial.html 中。这是
的第二个slug是
slug
tutorialTopicCategory
模型,该模型在 models.py.py.py 中。因此,要访问它,您需要在模板 tutorial.html 中编写topor.tutorial_topic_category.slug
。它的第三个sl是
tutorial_slug
,这意味着您的slug
topic>主题
模型,该模型是 models.py.py.py 的最后一个。因此,要访问它,您需要在模板 tutorial.html 中简单地编写topic.slug
。只需更改此信息,不要更改任何内容:
topic.html或模板文件:
By default, if you don't give anything in route while defining in path() as you give
"<main_category_slug>/<topic_category_slug>/<tutorial_slug>"
so these are considered strings.For example:
<str:name>/
and<name>/
is same.If you want to make slugs all of three, so you should define them as slug first, write your route as
"<slug:main_category_slug>/<slug:topic_category_slug>/<slug:tutorial_slug>/"
, then these is considered as slugs.Minor Mistake: You should not define same variables, it must be
for topic in topics
not.for topic in topic
Edit:
You should do following things in your code:
Firstly, change this link:
To this link:
Why?
Look at your url correctly:
It's first slug is
main_category_slug
that means slug ofTutorialCategory
model which is at the first in models.py. So, for accessing you need to writetopic.tutorial_topic_category.tutorial_category.slug
in the template tutorial.html.It's second slug is
topic_category_slug
that meansslug
ofTutorialTopicCategory
model which is at the second in models.py. So, for accessing it, you need to writetopic.tutorial_topic_category.slug
in the template tutorial.html.It's third slugs is
tutorial_slug
that means yourslug
ofTopic
model which is at the last in models.py. So, for accessing it, you need to writetopic.slug
simply, in the template tutorial.html.Simply change this and don't change anything:
topic.html or template file: