英文:
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实体转义符(如<
和>
)还原为正常的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 = '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>
答案1
得分: 0
你正在发送一个 Queryset 作为 posts = blog_Post.objects.filter(published_date__lte=timezone.now(), status=1, pk=pid)
,尽管可能只有一个结果,但它仍然是一个需要进行评估的 Queryset。可以尝试以下方式:
{% for p in post %}
<li><a href="{% url 'blog:single' pid=p.id %}">单篇博客</a></li>
{% 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)
重要提示
这一行
<a href="{% url 'blog:single' pid=post.id %}">
很可能导致 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 %}
<li><a href="{% url 'blog:single' pid=p.id %}">Blog Single</a></li>
{% 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
<a href="{% url 'blog:single' pid=post.id %}">
most likely has post.id
as none, which is what is causing the error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论