I am makeing a blog with Django. When I try to go to a specific blog post I get this error: NoReverseMatch at /blog/view/None/ Reverse for 'blog_like' not found. 'blog_like' is not a valid view function or pattern name.
I don't know what is causing this issue at all. Any help would be appreciated
full error:
File "C:\Users\sekoc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\sekoc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\sekoc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\sekoc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\template\defaulttags.py", line 446, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\sekoc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\urls\base.py", line 86, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "C:\Users\sekoc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\urls\resolvers.py", line 694, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'blog_like' not found. 'blog_like' is not a valid view function or pattern name.
[24/Apr/2022 17:30:28] "GET /blog/view/None/ HTTP/1.1" 500 155689
views.py
from django.shortcuts import render, reverse
from django.http import HttpResponseRedirect
from django.views import generic
from . import models
from django.contrib.auth import get_user_model
User = get_user_model()
from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin
# Create your views here.
class create_blog_post(generic.CreateView, LoginRequiredMixin):
model = models.Blog_Post
template_name = 'blog_app/creat_post.html'
fields = ('post_title', 'blog_content')
success_url = reverse_lazy('blog_app:all')
class view_blog_post(generic.ListView):
model = models.Blog_Post
template_name = 'blog_app/view_post.html'
class delet_blog_post(generic.DeleteView, LoginRequiredMixin):
model = models.Blog_Post
template_name = 'blog_app/delete_post.html'
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(user_id = self.request.user.id)
def delete(self, *args, **kwargs):
messages.success(self.request, 'Post Deleted')
return super().delete(*args, *kwargs)
class all_blog_posts(generic.ListView):
model = models.Blog_Post
template_name = 'blog_app/all_posts.html'
def postLike(request, pk):
post_id = request.POST.get('blog-id')
post = Blog.objects.get(pk=post_id)
ip = get_client_ip(request)
if not IpModel.objects.filter(ip=ip).exists():
IpModel.objects.create(ip=ip)
if post.likes.filter(id=IpModel.objects.get(ip=ip).id).exists():
post.likes.remove(IpModel.objects.get(ip=ip))
else:
post.likes.add(IpModel.objects.get(ip=ip))
return HttpResponseRedirect(reverse('post_detail', args=[post_id]))
view_post.html
{% extends "base.html" %}
{% block content %}
<div >
<h1>{{blog_post.post_title}}</h1>
<p>{{blog_post.blog_content}}</p>
<form action="{% url 'blog_like' pk=blog_post.id %}" method="post">
<button type="submit" name="blgo_id" value={{post.id}}><span aria-hidden="true"></span>Like</button>
</form>
</div>
{% endblock %}
This text was added to ignore "it looks like your post is mostly code; please add some more details"
CodePudding user response:
action="{% url 'blog_like' pk=blog_post.id %}"
Your urls.py
file does not have a url that is named "blog_like".