我有几篇文章,并且在查看它们时,有必要计算每篇文章的这些视图的数量。并显示对文章有多少意见 enter image description here 例如,我想通过单击链接将其添加到“ view = models.Integerfiels” 1
我的模型。py
from django.db import models
from taggit.managers import TaggableManager
class Articles(models.Model):
title = models.CharField(max_length= 200)
post = models.TextField()
date = models.DateTimeField()
img = models.ImageField(upload_to='', default="default_value")
tags = TaggableManager()
article_like = models.IntegerField(default='0')
article_dislike = models.IntegerField(default='0')
view = models.IntegerField(default='0')
font_size = models.IntegerField()
def __str__(self):
return self.title
views.py
class ArticleIndex(ListView):
model = Articles
template_name = 'news/posts.html'
class ArticleDetail(DetailView):
model = Articles
template_name = 'news/post.html'
urls.py
urlpatterns=[
path('', ArticleIndex.as_view(), name='articles_list'),
path('<int:pk>/', ArticleDetail.as_view(), name='article_detail'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 0 :(得分:1)
我建议您在此处创建自己的视图
path('your_url/<int:pk>/', views.ArticleDetailView, name='article_detail'),
在您的url.py中,您这样做:
<a href="{{ article.pk }}">{{ article }}</a>
在您的forloop模板中,您只需要将所有pk添加到锚标签的href中,就像这样:
{
//"terminal.integrated.fontFamily": "'Ubuntu Mono derivative Powerline', 'fontawesome'",
"terminal.integrated.fontFamily": "'Meslo LG M DZ for Powerline', 'fontawesome'",
"terminal.integrated.fontSize": 16
}