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,
/blog/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
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
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 ???????? thanks to those print('positive), and print('negative') of views.py I know it is always taken positive_post func from views.
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. 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
. So Django will fire the first view that matches in the list of paths.
You should make the paths non-overlapping, for example with:
path('<int:post_id>/',views.positive_post, name='positive_post'),
path('<int:post_id>/',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. Normally POST, PUT, PATCH, and DELETE requests are used for this. In that case you make a small
<form>
that will trigger a POST request, or you use some AJAX calls.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.