Dear all,
My question is about “HttpResponseRedirect reverse” when using like button on a page.
I am making travel pages. I want to click a “like button” on city detail page and redirect reverse to city detail page again.
Here are my details:
Travel URL pattern:
path('travel/Usa/<slug:state_slug>/<slug:city_slug>/<slug:detail_slug>/', views.citydetail, name='citydetail'),
“like button” on citydetail page
Like URL pattern:
path('<slug:detail_slug>/likeusa/', views.likeusa, name='likeusa'),
Mode:
class Citydetail(models.Model):
country = models.ForeignKey(Country, on_delete=models.CASCADE)
state = models.ForeignKey(State, on_delete=models.CASCADE)
city = models.ForeignKey(City, on_delete=models.CASCADE)
citydetail = RichTextField(null=True, blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
likes = models.IntegerField(default=0)
etc.......
def get_urlj(self):
return reverse('citydetail',args=[self.state.slug, self.city.slug, self.slug])
def __str__(self):
return "{0}.{1}".format(self.id, self.citydetail)
Like
class Likes(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_like')
post = models.ForeignKey(Citydetail, on_delete=models.CASCADE, related_name='post_like')
def user_liked_post(sender, instance, *args, **kwargs):
like = instance
post = like.post
sender = like.user
def user_unlike_post(sender, instance, *args, **kwargs):
like = instance
post = like.post
sender = like.user
# Likes
post_save.connect(Likes.user_liked_post, sender=Likes)
post_delete.connect(Likes.user_unlike_post, sender=Likes)
Views
def citydetail(request, state_slug, city_slug, detail_slug ):
user = request.user
post=get_object_or_404(Citydetail,state__slug=state_slug,city__slug=city_slug,detail__slug=detail_slug)
profile = Profile.objects.get(user=user)
return render(request, 'travel/usa/citydetail.html', locals())
@login_required
def like(request, detail_slug):
user = request.user
post = Citydetail.objects.get(slug=detail_slug)
current_likes = post.likes
liked = Likes.objects.filter(user=user, post=post).count()
if not liked:
like = Likes.objects.create(user=user, post=post)
#like.save()
current_likes = current_likes + 1
else:
Likes.objects.filter(user=user, post=post).delete()
current_likes = current_likes - 1
post.likes = current_likes
post.save()
return HttpResponseRedirect(reverse('citydetail', args=[detail_slug]))
(I dont know how to reverse to detail page )
citydetail html
<div class="media-right">
<a href="{% url 'likeusa' detail_slug %}"> <i class="material-icons">favorite</i></a>
<p>{{ post.likes }}likes</p>
</div>
DB:
suppose my urlpattern is like /usa/texas/huston/detail/
Error:
NoReverseMatch at /detail/like/
Reverse for 'detail' with arguments '('detail',)' not found. 1 pattern(s) tried: ['usa/(?P<state_slug>[-a-zA-Z0-9_]+/(?P<city_slug>[-a-zA-Z0-9_]+)/+)/(?P<detail_slug>[-a-zA-Z0-9_]+)/\\Z']
How could I Make “HttpResponseRedirect reverse” directly to detail page when click “like icon” on detail page.
Best regards