如何将slug参数添加到django中的URL?

发布于 2025-01-31 04:47:45 字数 3041 浏览 2 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零時差 2025-02-07 04:47:45

默认情况下,如果您在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。

小错误:您不应定义相同的变量,对于主题中的主题> 而不是 ,主题中的主题

注意:始终在每个路由的尽头给/

编辑:

您应该在代码中进行以下操作:

首先,将此链接更改为:

<a href="{% url 'tutorials:tutorial' topic.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %}">{{topic.title}} - Start Now</a>

转到此链接:

<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,它意味着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或模板文件:

{% 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 %}

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.

Note: Always give / at the end of every route.

Edit:

You should do following things in your code:

Firstly, change this link:

<a href="{% url 'tutorials:tutorial' topic.tutorial_category.slug topic.tutorial_topic_category.slug topic.slug %}">{{topic.title}} - Start Now</a>

To this 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>

Why?

Look at your url correctly:

 path("<slug:main_category_slug>/<slug:topic_category_slug>/<slug:tutorial_slug>/", views.tutorial, name='tutorial')

It's first slug is main_category_slug that means slug of TutorialCategory model which is at the first in models.py. So, for accessing you need to write topic.tutorial_topic_category.tutorial_category.slug in the template tutorial.html.

It's second slug is topic_category_slug that means slug of TutorialTopicCategory model which is at the second in models.py. So, for accessing it, you need to write topic.tutorial_topic_category.slug in the template tutorial.html.

It's third slugs is tutorial_slug that means your slug of Topic model which is at the last in models.py. So, for accessing it, you need to write topic.slug simply, in the template tutorial.html.

Simply change this and don't change anything:

topic.html or template file:

{% 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 %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文