python - I need that when I click on an article <> it’s called the name of the article
问题描述
I need that when I click on an article <> it’s called the name of the article
For example, now, when I click on an article, I go to url address news / 1, but I need instead of 1, there was a name similar to this picture. Or here’s another premiere, I call the article “How to Skip School” and url the address will be like this news/how-to-skip-school
views.py
class ArticleIndex(ListView):
model = Articles
template_name = 'news/posts.html'
def ArticleDetailView(request, pk):
tag=None
Articles.objects.filter(pk=pk).update(view=F('view') + 1)
Articles.objects.all()
article_details = Articles.objects.filter(pk=pk).first()
if request.method == 'POST':
comment_form = Comments(request.POST)
comment_form.save()
else:
comment_form = Comments()
commentss = CommentModel.objects.all()
return render(request, 'news/post.html', {'article_details': article_details,
'comment_form': comment_form, 'comments': commentss,
'tag': tag
})
urls.py
path('', ArticleIndex.as_view(), name='articles_list'),
path('<int:pk>/', views.ArticleDetailView, name='article_detail'),
models.py
class Articles(models.Model):
title = models.CharField(max_length= 200)
post = models.TextField()
date = models.DateTimeField()
img = models.ImageField(upload_to='', default="default_value",verbose_name='Каритинка 260х180')
tags = TaggableManager()
article_like = models.IntegerField(default='0')
article_dislike = models.IntegerField(default='0')
view = models.IntegerField(default='0')
datesArticle = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-datesArticle']
def __str__(self):
return self.title
解决方案
这称为蛞蝓。您可以将 slug 添加到模型中,例如使用SlugField
[Django-doc],但最好安装django-autoslug
package并AutoSlugField
改用:
from django.db import models
from autoslug import AutoSlugField
class Articles(models.Model):
title = models.CharField(max_length= 200)
slug = AutoSlugField(populate_from='title')
post = models.TextField()
date = models.DateTimeField()
img = models.ImageField(upload_to='', default='default_value', verbose_name='Каритинка 260х180')
tags = TaggableManager()
article_like = models.IntegerField(default='0')
article_dislike = models.IntegerField(default='0')
view = models.IntegerField(default='0')
datesArticle = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-datesArticle']
def __str__(self):
return self.title
你可以改变你的 URL 模式来解析这样的 slug:
path('', ArticleIndex.as_view(), name='articles_list'),
path('<slug:slug>/', views.article_detail_view, name='article_detail'),
在您看来,您可以使用以下方法处理 slug:
def ArticleDetailView(request, slug):
tag=None
Articles.objects.filter(slug=slug).update(view=F('view') + 1)
article_details = Articles.objects.filter(slug=slug).first()
if request.method == 'POST':
comment_form = Comments(request.POST)
if comment_form.is_valid():
comment_form.save()
return redirect('some-view-name')
else:
comment_form = Comments()
comments = CommentModel.objects.all()
return render(
request,
'news/post.html',
{
'article_details': article_details,
'comment_form': comment_form,
'comments': comments,
'tag': tag
}
)
请注意,您应该检查表单是否有效,如果成功,您最好重定向到另一个视图,以实现Post/Redirect/Get模式 [wiki]。
推荐阅读
- ios - 滑动关闭时输入附件视图不粘在键盘上
- ios - 升级到 React Native 0.63.2 时出现黄框错误
- node.js - 使用 Nodejs Zlib 压缩从 Multer 上传的文件
- java - 有没有办法根据对象字段的唯一值的数量将对象列表拆分为 n 个列表?
- python - 是否有 networkx 函数来计算社区之间的边数?
- r - 绘制r中线性回归的限制三次样条图
- .net - “但不包含”的正则表达式
- jquery - 如何在 jQuery Timepicker 中修改 startTime?
- angular - Angular 使用带有 createElement 的选择器
- c++ - 在具有其他整数成员变量的类中将 2D char 数组声明为 c++ 中的大小