首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我在/blog/view/None/得到错误NoReverseMatch

我在/blog/view/None/得到错误NoReverseMatch
EN

Stack Overflow用户
提问于 2022-04-24 21:36:21
回答 1查看 45关注 0票数 0

我要和Django一起写博客。当我试图访问一个特定的博客文章时,我得到了一个错误:NoReverseMatch at /blog/view/None/ Reverse for 'blog_like' not found. 'blog_like' is not a valid view function or pattern name.,我根本不知道是什么导致了这个问题。如能提供任何帮助,将不胜感激。

完全错误:

代码语言:javascript
复制
  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

代码语言:javascript
复制
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

代码语言:javascript
复制
{% extends "base.html" %}

{% block content %}
<div class="jumbotron">
  <h1>{{blog_post.post_title}}</h1>
  <p>{{blog_post.blog_content}}</p>
  <form class="form-inline" action="{% url 'blog_like' pk=blog_post.id %}" method="post">
<button type="submit" class="btn btn-default btn-lg like_button" name="blgo_id" value={{post.id}}><span class="glyphicon glyphicon glyphicon-thumbs-up" aria-hidden="true"></span>Like</button>
  </form>
</div>
{% endblock %}

EN

回答 1

Stack Overflow用户

发布于 2022-04-24 21:39:46

代码语言:javascript
复制
action="{% url 'blog_like' pk=blog_post.id %}"

您的urls.py文件没有名为"blog_like“的url。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71992529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档