【问题标题】:Reverse for 'post_share' with arguments '('',)' not found. 1 pattern(s) tried: ['post_share/(?P<post_id>[0-9]+)$']未找到带有参数 '('',)' 的 'post_share' 的反向操作。尝试了 1 种模式:['post_share/(?P<post_id>[0-9]+)$']
【发布时间】:2023-03-28 05:51:01
【问题描述】:

views.py

def post_share(request, post_id):
    post = get_object_or_404(Post,id=post_id)
    sent = False

    return render(request, 'mains/post_share.html', {'post':post,'sent':sent})

urls.py

    path('post_share/<int:post_id>',views.post_share,name='post_share'),

show_more.html

  <p><a href="{% url 'mains:post_share' post.id %}">Share this post</a></p>



def __str__(self):
    return self.post_title

当我点击 分享这篇文章(链接)在 share_more.html Reverse for 'post_share' with arguments '('',)' not found .尝试了 1 种模式:['post_share/(?P[0-9]+)$'].

【问题讨论】:

  • posts更改为post
  • @minglyu 我已经改了。但仍然出现错误。
  • 你确定post_share在mains app的urls.py中吗?
  • 是的,我 100% 确定。有没有发现什么错误?
  • @Progam 你在重复这个错误,我猜你上次也遇到了同样的问题,检查我下面的答案,努力练习,仔细阅读文档,多注意代码。

标签: html django django-views django-forms django-templates


【解决方案1】:

您的网址需要 post_id kwarg;

 <p><a href="{% url 'mains:post_share' post_id=post.id %}">Share this post</a></p>

【讨论】:

  • 您的 post.id 为 None,该帖子很可能已被删除
  • post.id 没有返回 id,从后端检查
  • 您传递给 HTML 的 post 上下文变量检查。将呈现

    分享此帖子

    的视图代码添加到您的问题中。
  • 呈现此帖子的视图

    分享此帖子

    HTML 页面,请将该视图添加到您的问题中
  • @Progam 我再说一遍,哪个视图正在渲染 show_more.html ?不是 post_share.html 。为您呈现 show_more.html 的问题添加视图
【解决方案2】:

由于模板中的“post.id”问题,我遇到了同样的错误,它已针对不存在的项目。检查这条线 &lt;p&gt;&lt;a href="{% url 'mains:post_share' post.id %}"&gt;Share this post&lt;/a&gt;&lt;/p&gt; 从哪里获取 post.id

【讨论】:

    猜你喜欢
    • 2021-04-24
    • 1970-01-01
    • 2017-11-05
    • 2018-11-21
    • 2020-08-16
    • 1970-01-01
    • 2022-01-13
    • 2023-03-18
    • 2022-08-05
    相关资源
    最近更新 更多