如何在URL中使用通用视图中的slug?(How to use slug in URL with generic view?)

编程入门 行业动态 更新时间:2024-10-28 04:20:16
如何在URL中使用通用视图中的slug?(How to use slug in URL with generic view?)

我如何通过它的标题来查看单篇文章,如下所示: news/article_name ?

我设法使用数字( <int:pk>在urls中,然后使用{% url 'article' article.id %}在模板中访问它),所以news/1实际上起作用。 而不是使用数字,我想使用标题,并不知道如何。

models.py

class Article(models.Model): headline = models.CharField(max_length=200) date_created = models.DateTimeField(auto_now_add=True) date_updated = models.DateTimeField(auto_now=True) author = models.ForeignKey(User, on_delete=models.CASCADE) content = models.TextField() def __str__(self): return self.headline

urls.py

path('<slug:headline>/', views.DetailView.as_view(), name='article'),

views.py

class ArticleView(generic.DetailView): model = Article template_name = 'news/index.html' context_object_name = 'article' local_context = {**context, **{'show_comments':False}}

在文章模板中的某处

<p><a href="{% url 'article' %}">Read more</a></p>

How can I view a single article by it's headline like so: news/article_name?

I managed to make it work with numbers (<int:pk> in urls then access it in template using {% url 'article' article.id %}) so news/1 actually worked. Instead of using numbers I want to use headlines and can't figure out how.

models.py

class Article(models.Model): headline = models.CharField(max_length=200) date_created = models.DateTimeField(auto_now_add=True) date_updated = models.DateTimeField(auto_now=True) author = models.ForeignKey(User, on_delete=models.CASCADE) content = models.TextField() def __str__(self): return self.headline

urls.py

path('<slug:headline>/', views.DetailView.as_view(), name='article'),

views.py

class ArticleView(generic.DetailView): model = Article template_name = 'news/index.html' context_object_name = 'article' local_context = {**context, **{'show_comments':False}}

somewhere in article template

<p><a href="{% url 'article' %}">Read more</a></p>

最满意答案

当您使用主键时,必须将其包含在URL标记中。

{% url 'article' article.id %}

同样,你必须在URL标签中加入标题。

{% url 'article' article.headline %}

请注意,包含单独的slug字段而不是使用headline可能会更好,因此您可以使用/news/man-bites-dog而不是/news/Man%20Bites%20Dog

class Article(models.Model): headline = models.CharField(max_length=200) slug = models.SlugField()

然后你会将url标签更改为:

{% url 'article' article.slug %}

When you used the primary key you had to include that in the URL tag.

{% url 'article' article.id %}

In the same way, you have to include the headline in the URL tag.

{% url 'article' article.headline %}

Note that it might be better to include a separate slug field instead of using headline, so that you get URLs like /news/man-bites-dog instead of /news/Man%20Bites%20Dog

class Article(models.Model): headline = models.CharField(max_length=200) slug = models.SlugField()

Then you would change the url tag to:

{% url 'article' article.slug %}

更多推荐

本文发布于:2023-08-07 13:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464752.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   如何在   URL   view   slug

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!