首页 > 解决方案 > 在 django 中单击时重定向到各种博客文章的问题

问题描述

我正在尝试创建一个简单的博客,在主页上显示各种文章。截至目前的索引页面包含各种文章的标题和副标题。我希望它在点击时显示文章的全部内容。这是我在主页上遇到的错误。

NoReverseMatch at /
Reverse for 'article' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<article_id>[0-9]+)$']

这是urls.py我创建的页面应用程序中的内容。

from django.urls import path
from . import views

urlpatterns=[

    path('',views.index,name='index'),
    path('<int:article_id>',views.article,name='article'),
    path('about',views.about,name='about'),

]

这是我的意见.py

from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse
from . models import Post
# Create your views here.
def index(request):
    post=Post.objects.all()

    context = {
        'post' : post
    }

    return render(request,'pages/index.html',context)

def article(request,article_id):

    article=get_object_or_404(Post,pk=article_id)

    context = {
        'article' : article
    }
    return render(request,'pages/article.html',context)


def about(request):
    return render(request,'pages/about.html')

正如您可能看到的,我通过 article_id 引用文章的内容,并且有关特定帖子的数据是从数据库中获取的。

这是我的 index.html,它应该在点击时重定向到特定帖子的内容。

    {%extends 'base.html'%}
    {%load static%}
    {%block content%}

    <!-- Page Header -->
    <header class="masthead" style="background-image: url({% static 'img/home-bg.jpg' %})">
      <div class="overlay"></div>
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-10 mx-auto">
            <div class="site-heading">
              <h1>Clean Blog</h1>
              <span class="subheading">A Blog Theme by Start Bootstrap</span>
            </div>
          </div>
        </div>
      </div>
    </header>

<!-- Posts -->

    {% if post %}
      {% for posts in post %}
        <div class="container">
          <div class="row">
            <div class="col-lg-8 col-md-10 mx-auto">
              <div class="post-preview">
                <a href="{% url 'article' article.id%}">
                  <h2 class="post-title">
                    {{posts.title}}
                  </h2>
                  {%if posts.subtitle%}
                    <h3 class="post-subtitle">
                      {{posts.subtitle}} 
                    </h3>
                  {%endif%}
                </a>
                <p class="post-meta">Posted by
                  <a href="#">{{posts.postby}}</a>
                  on {{posts.date}}</p>
              </div>
              <hr>
            </div>
          </div>
        </div>


      {% endfor %}
    {%endif%}       
            <!-- Pager -->
          <div class="container">
            <div class="row">
              <div class="col-lg-8 col-md-10 mx-auto">
                <div class="clearfix">
                  <a class="btn btn-primary float-right" href="#">Older Posts &rarr;</a>
                </div>
              </div>
            </div>
          </div>

      <hr>
    {%endblock%} 

但是,当我手动输入 localhost:8000/1 或 localhost:8000/2 时,我得到了所需的页面,就像我想要的那样。但问题是,它不会在点击时重定向。我最好的猜测是这个

<a href="{% url 'article' article.id%}">

正在制造问题。

欢迎所有建议!谢谢你。

标签: pythondjangodjango-templates

解决方案


您在该模板中没有任何称为“文章”的内容。您的对象称为“帖子”。所以:

<a href="{% url 'article' posts.id %}">

(我们将撇开您为什么将您的帖子集合称为“帖子”而将每个帖子称为“帖子”的问题......)


推荐阅读