如何修复Django中一堆无反向数学错误。

huangapple go评论70阅读模式
英文:

How to fix bunch of no reverse math errors in Django

问题

我已经为您翻译好了代码部分,如下所示:

# urls.py

from django.urls import path, include
from blog.views import *

app_name = 'blog'

urlpatterns = [
    path('', blog_view, name='index'),
    path('single/<int:pid>', blog_single, name='single')
]

# views.py

from django.shortcuts import render, get_object_or_404
from blog.models import blog_Post
from django.utils import timezone

# Create your views here.

def blog_view(request, pid):
    posts = blog_Post.objects.filter(published_date__lte=timezone.now(), status=1, pk=pid)
    context = {'posts': posts}
    return render(request, "blog/blog-home.html", context)

def blog_single(request, pid):
    blog_object = get_object_or_404(blog_Post, pk=pid)
    blog_object.counted_views += 1
    blog_object.save()
    return render(request, "blog/blog-single.html", {'posts': blog_object})

# base.html

<li><a href="{% url 'blog:index' %}">Blog Home</a></li>

<li><a href="{% url 'blog:single' pid=post.id %}">Blog Single</a></li>

请注意,我已将代码中的HTML实体转义符(如&lt;&gt;)还原为正常的HTML标签。如果您需要进一步的帮助,请随时提出。

英文:

I read everything about no reverse match error but nothings changed. My task is "In the view related to the list of posts that are filtered, how to filter the posts based on the time that is considered for publication. This filter should be such that it takes the time considered for the published_date section, and compares it with the current time. If it is past the current time, it should be displayed and otherwise not." and "In the view related to displaying a single post, insert a function that every time the view related to each post is called, one unit will be added to the counted_view attribute of that post."

# urls.py

from django.urls import path, include
from blog.views import *

app_name = &#39;blog&#39;

urlpatterns = [
    path(&#39;&#39;, blog_view, name=&#39;index&#39;),
    path(&#39;single/&lt;int:pid&gt;&#39;, blog_single, name=&#39;single&#39;)
] 


# views.py 

from django.shortcuts import render, get_object_or_404
from blog.models import blog_Post
from django.utils import timezone
# Create your views here.

def blog_view(request, pid):
    posts = blog_Post.objects.filter(published_date__lte=timezone.now(), status=1, pk=pid)
    context = {&#39;posts&#39;: posts}
    return render(request, &quot;blog/blog-home.html&quot;,context)

def blog_single(request, pid):
    
    
    blog_object = get_object_or_404(blog_Post, pk=pid)
    blog_object.counted_views += 1
    blog_object.save()
    
        
return render(request, &quot;blog/blog-single.html&quot;, {&#39;posts&#39;:blog_object})]

base.html

&lt;li&gt;&lt;a href=&quot;{% url &#39;blog:index&#39;   %}&quot;&gt;Blog Home&lt;/a&gt;&lt;/li          

 &lt;li&gt;&lt;a href=&quot;{% url &#39;blog:single&#39; pid=post.id %}&quot;&gt;Blog Single&lt;/a&gt;&lt;/li&gt;


答案1

得分: 0

你正在发送一个 Queryset 作为 posts = blog_Post.objects.filter(published_date__lte=timezone.now(), status=1, pk=pid),尽管可能只有一个结果,但它仍然是一个需要进行评估的 Queryset。可以尝试以下方式:

{% for p in post %}
    &lt;li&gt;&lt;a href=&quot;{% url &#39;blog:single&#39; pid=p.id %}&quot;&gt;单篇博客&lt;/a&gt;&lt;/li&gt;
{% endfor %}

或者,你可以使用 post = blog_Post.objects.get(published_date__lte=timezone.now(), status=1, pk=pid),这将返回一个实例,尽管我建议确保该对象实际存在,所以可能要这样做:

post = blog_Post.objects.get_object_or_404(published_date__lte=timezone.now(), status=1, pk=pid)

重要提示
这一行

&lt;a href=&quot;{% url &#39;blog:single&#39; pid=post.id %}&quot;&gt;

很可能导致 post.id 为 None,这是导致错误的原因。

英文:

You are sending a Queryset as posts = blog_Post.objects.filter(published_date__lte=timezone.now(), status=1, pk=pid), and although there may be only ONE, it is still a Queryset, which needs to be evaluated. Something like:

{% for p in post %}
    &lt;li&gt;&lt;a href=&quot;{% url &#39;blog:single&#39; pid=p.id %}&quot;&gt;Blog Single&lt;/a&gt;&lt;/li&gt;
{% endfor %}

Or, you can use post = blog_Post.objects.get(published_date__lte=timezone.now(), status=1, pk=pid), which will return ONE instance, although I would make sure that that object actaully exists, so maybe do

post = blog_Post.objects.get_object_or_404(published_date__lte=timezone.now(), status=1, pk=pid)

bottom line*
The line

&lt;a href=&quot;{% url &#39;blog:single&#39; pid=post.id %}&quot;&gt;

most likely has post.id as none, which is what is causing the error.

huangapple
  • 本文由 发表于 2023年8月5日 01:19:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定