Home > Net >  django.urls.exceptions.NoReverseMatch: Reverse for 'index' with arguments '(5,)'
django.urls.exceptions.NoReverseMatch: Reverse for 'index' with arguments '(5,)'

Time:01-26

Django problem

views.py

def likes_up(request,post_id):
    post = get_object_or_404(Post, id=request.POST.get('post_id'))

   
    if post.like.filter(id=request.user.id).exists():
        post.like.remove(request.user)
    else:
        post.like.add(request.user)

    return HttpResponseRedirect(reverse('index', args=[post_id]))`

models.py

class Post(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    post = models.CharField(max_length=500,null=True,blank=True)
    timestamp = models.DateField(auto_now_add=True)
    like = models.ManyToManyField(User,blank=True,related_name="liked_user")

    def number_of_likes(self):
        return self.like.count()`

index.html

 {% if user.is_authenticated %}
     <form action="{% url 'like-post' pst.id%}" method="POST">
    {% csrf_token %}

    {% if post_is_liked %}
    <button type="submit" name="post_id" value="{{pst.id}}" >Unlike</button>
    {% else %}
    <button type="submit" name="post_id" value="{{pst.id}}" >Like</button>
    {% endif %}
  </form>

{% endif %}
<strong >{{ number_of_likes }} Like{{ number_of_likes|pluralize }}</strong>`

I try to make post like and unlike button and ı want to show page that how many users liked post?

But I got this error: How can I solve this problem?

This is my urls.py: urls.py


urlpatterns = [
    path("register/", views.register,name="register"),
    path("login/",views.login_view,name="login"),
    path("index/",views.index,name="index"),
    path("logout", views.logout_view, name="logout"),
    path('addpost/', views.addPost,name="addPosts"),
    path("edit-post/<int:post_id>", views.edit_post,name="edit-post"),
    path("delete/<int:post_id>", views.delete,name="delete"),
    path("like/<int:post_id>",views.likes_up,name="like-post")
]

My error is :

Internal Server Error: /like/5
Traceback (most recent call last):
  File "C:\Users\OSMAN MERT\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\OSMAN MERT\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\OSMAN MERT\Desktop\env\blogs\mysite\blog\views.py", line 102, in likes_up
    return HttpResponseRedirect(reverse('index', args=[post_id]))
  File "C:\Users\OSMAN MERT\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\base.py", line 88, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
  File "C:\Users\OSMAN MERT\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 802, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'index' with arguments '(5,)' not found. 1 pattern(s) tried: ['index/\\Z']

CodePudding user response:

You are trying to return your index view with an argument:

return HttpResponseRedirect(reverse('index', args=[post_id]))

But in your urls view index does not take any parameter:

path("index/", views.index, name="index")

You need to be specific and implement to reverse exactly what you have designed.

  • Related