保持随机性在django博客页面中尝试在每个帖子下显示评论部分时,没有反向匹配错误?

问题描述

error message

用户帖子是一个页面,已登录用户可以在其中查看自己的帖子,并且可以正常运行。我不知道为什么django说错误是在页面的html导航栏附近的基本模板中。

这是我的评论模型

class comment(models.Model):
linkedpost = models.ForeignKey(Post,related_name="postcomments",on_delete=models.CASCADE)
commentauthor = models.ForeignKey(User,on_delete=models.CASCADE)
body = models.TextField(max_length=100)
date_posted = models.DateTimeField(default=timezone.Now)

def __str__(self):
    return self.linkedpost.title

我的主页模板,博客帖子在其中循环浏览,并且每个帖子下面的评论也在循环浏览。

{% extends "blog/base.html" %}
{% block content %}
{% for post in posts %}
    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
      <div class="media-body">
        <div class="article-Metadata">
          <a class="mr-2" href="{% url 'user-posts' post.author.username %}">{{ post.author }}</a>
          <small class="text-muted">{{ post.date_posted|date:"F d,Y" }}</small>
        </div>
        <h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.title }}</a></h2>
        <p class="article-content">{{ post.content }}</p>
        <div>
            <h2>Comments</h2>
            {% for cmnts in post.postcomments.all %}
                #<a class="mr-2" href="{% url 'user-posts' cmnts.author.username %}">{{ cmnts.commentauthor }}</a>
                <small class="text-muted">{{ cmnts.date_posted|date:"F d,Y" }}</small>
                <p class="article-content">{{ cmnts.body }}</p>
            {% endfor %}
        </div>
      </div>
    </article>
{% endfor %}

Urls.py项目

urlpatterns = [
path('admin/',admin.site.urls),path('register/',user_views.register,name='register'),path('login/',auth_views.LoginView.as_view(template_name='users/loginpage.html'),name='login'),path('logout/',auth_views.logoutView.as_view(template_name='users/logout.html'),name='logout'),path('passwordreset/',auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'),name='passwordreset'),path('passwordreset/done/',auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'),name='passwordresetdone'),path('passwordresetconfirm/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'),name='passwordresetconfirm'),path('profile/',user_views.profile,name='profile'),path('about/',blog_views.about,name='blog-about'),path('',include('blog.urls')),

]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

博客应用urls.py

urlpatterns = [
path('',PostListView.as_view(),name='blog-home'),path('user/<str:username>/',UserPostListView.as_view(),name='user-posts'),path('post/<int:pk>',PostDetailView.as_view(),name='post-detail'),path('post/new/',PostCreateView.as_view(),name='post-create'),path('post/<int:pk>/update/',PostUpdateView.as_view(),name='post-update'),path('post/<int:pk>/delete/',PostDeleteView.as_view(),name='post-delete'),path('team/',views.team,name='blog-team')

]

这些是基于主类的视图,用于显示帖子和用户帖子。

class PostListView(ListView):
    model = Post
    template_name = 'blog/home.html'
    context_object_name = 'posts'
    ordering = ['-date_posted']
    paginate_by = 5

class UserPostListView(ListView):
    model = Post
    template_name = 'blog/user_posts.html'
    context_object_name = 'posts'
    paginate_by = 5

    def get_queryset(self):
        user = get_object_or_404(User,username=self.kwargs.get('username'))
        return Post.objects.filter(author=user).order_by('-date_posted')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)