[英]django vote system failing
I am Confused.我很迷惑。 I am trying to set a vote system for a post in a blog.
我正在尝试为博客中的帖子设置投票系统。 But Django always sums on the vote posive side,
但是 Django 总是在投票积极的一面进行总结,
/blog/models.py /博客/models.py
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
unique_for_date='publish')
author = models.ForeignKey(User,
on_delete=models.CASCADE,
related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices=STATUS_CHOICES,
default='draft')
post_pos = models.IntegerField(default=0)
post_neg = models.IntegerField(default=0)
objects = models.Manager() # The default manager.
published = PublishedManager() # Our custom manager.
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail',
args=[self.publish.year,
self.publish.month,
self.publish.day, self.slug])
/blog/urls.py /博客/urls.py
urlpatterns = [
path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',
views.post_detail,
name='post_detail'),
path('<int:post_id>/share/',views.post_share, name='post_share'),
path('<int:post_id>/',views.positive_post, name='positive_post'),
path('<int:post_id>/',views.negative_post, name='negative_post'),
]
blog/views.py博客/views.py
def positive_post(request,post_id):
obj = Post.objects.get(pk=post_id)
obj.post_pos += 1
obj.save()
print ('positive')
return redirect(obj.get_absolute_url())
def negative_post(request,post_id):
obj = Post.objects.get(pk=post_id)
obj.post_neg += 1
obj.save()
print ('negative')
return redirect(obj.get_absolute_url())
template:模板:
<p>
<a href="{% url 'blog:positive_post' post.id %}">
VOTE UP {{ post.post_pos}}
</p>
<p>
<a href="{% url 'blog:negative_post' post.id %}">
VOTE DOWN {{ post.post_neg}}
</p>
no matter if I click vote up or vote down always sums in obj.post_pos ????????无论我点击投赞成票还是投反对票,obj.post_pos 中的总和 ???????? thanks to those print('positive), and print('negative') of views.py I know it is always taken positive_post func from views.
多亏了views.py的print('positive)和print('negative'),我知道它总是从视图中获取positive_post func。
positive
[14/Sep/2020 19:04:12] "GET /blog/2/ HTTP/1.1" 302 0
[14/Sep/2020 19:04:12] "GET /blog/2020/8/17/post-number-2/ HTTP/1.1" 200 1284
Anyone knows what is happening??有谁知道这是怎么回事??
Both paths resolve to the same URL.两个路径都解析为相同的 URL。 Indeed if you look at what Django generates for
{% url 'blog:positive_post' post.id %}
and {% url 'blog:negative_post' post.id %}
, it will both return / post.id
, with post.id
the filled in .id
of the post
.实际上,如果您查看 Django 为
{% url 'blog:positive_post' post.id %}
和{% url 'blog:negative_post' post.id %}
,它都会返回/ post.id
和post.id
填写的.id
post
。 So Django will fire the first view that matches in the list of paths.因此 Django 将触发路径列表中匹配的第一个视图。
You should make the paths non-overlapping, for example with:您应该使路径不重叠,例如:
path('<int:post_id>/
up/',views.positive_post, name='positive_post'),
path('<int:post_id>/
down/',views.negative_post, name='negative_post'),
Note :Section 9 of the HTTP protocol specifies that requests like GET and HEAD should not have side-effets, so you should not change entities with such requests.
注:HTTP协议的第9节指定,像GET和HEAD请求不应该有副作用effets,所以你不应该这样的请求改变的实体。 Normally POST, PUT, PATCH, and DELETE requests are used for this.
通常 POST、PUT、PATCH 和 DELETE 请求用于此目的。 In that case you make a small
<form>
that will trigger a POST request, or you use some AJAX calls.在这种情况下,您可以创建一个小的
<form>
来触发 POST 请求,或者您使用一些 AJAX 调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.