在Django 1.11中模板渲染期间出错

时间:2017-06-12 09:43:55

标签: django django-templates django-urls

Django版本:1.11 Python版本:3.6.1 我正在尝试关注Openclassroom(Tuto)上的Django教程,我有一个无法解决的问题,我无法删除此错误

这是追溯:

File "C:\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
  41.             response = get_response(request)

File "C:\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\admin\crepes\blog\views.py" in accueil
  15.     return render(request, 'blog/accueil.html', {'articles': articles})

File "C:\Python36\lib\site-packages\django\shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "C:\Python36\lib\site-packages\django\template\loader.py" in render_to_string
  68.     return template.render(context, request)

File "C:\Python36\lib\site-packages\django\template\backends\django.py" in render
  66.             return self.template.render(context)

File "C:\Python36\lib\site-packages\django\template\base.py" in render
  207.                     return self._render(context)

File "C:\Python36\lib\site-packages\django\template\base.py" in _render
  199.         return self.nodelist.render(context)

File "C:\Python36\lib\site-packages\django\template\base.py" in render
  990.                 bit = node.render_annotated(context)

File "C:\Python36\lib\site-packages\django\template\base.py" in render_annotated
  957.             return self.render(context)

File "C:\Python36\lib\site-packages\django\template\loader_tags.py" in render
  177.             return compiled_parent._render(context)

File "C:\Python36\lib\site-packages\django\template\base.py" in _render
  199.         return self.nodelist.render(context)

File "C:\Python36\lib\site-packages\django\template\base.py" in render
  990.                 bit = node.render_annotated(context)

File "C:\Python36\lib\site-packages\django\template\base.py" in render_annotated
  957.             return self.render(context)

File "C:\Python36\lib\site-packages\django\template\loader_tags.py" in render
  72.                 result = block.nodelist.render(context)

File "C:\Python36\lib\site-packages\django\template\base.py" in render
  990.                 bit = node.render_annotated(context)

File "C:\Python36\lib\site-packages\django\template\base.py" in render_annotated
  957.             return self.render(context)

File "C:\Python36\lib\site-packages\django\template\defaulttags.py" in render
  216.                     nodelist.append(node.render_annotated(context))

File "C:\Python36\lib\site-packages\django\template\base.py" in render_annotated
  957.             return self.render(context)

File "C:\Python36\lib\site-packages\django\template\defaulttags.py" in render
  458.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "C:\Python36\lib\site-packages\django\urls\base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "C:\Python36\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
  497.         raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /blog/accueil
Exception Value: Reverse for 'blog_lire' with arguments '('',)' not found. 1 pattern(s) tried: ['blog/(?P<slug>.+)$']

一些代码:

models.py:

from django.db import models

class Article(models.Model):
  titre = models.CharField(max_length=100)
  slug = models.SlugField()
  auteur = models.CharField(max_length=42)
  contenu = models.TextField(null=True)
  date = models.DateTimeField(verbose_name="Date de parution",
                            auto_now_add=True, auto_now=False)
  is_visible = models.BooleanField(verbose_name="Article publié ?",
                                 default=False)
  categorie = models.ForeignKey('Categorie')

  def __str__(self):
    return self.titre

class Categorie(models.Model):
  titre = models.CharField(max_length=100)
  def __str__(self):
    return self.titre

views.py:

from django.shortcuts import render, get_object_or_404
from .models import Article

def accueil(request): 
  articles = Article.objects.filter(is_visible=True).order_by('-date')[:4]
  return render(request, 'blog/accueil.html', {'articles': articles})

def lire_article(request, slug):
   article = get_object_or_404(Article, slug=slug)
   return render(request, 'blog/lire_article.html', {'article': article})

博客/ urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
  url(r'^accueil$', views.accueil),
  url(r'^(?P<slug>.+)$', views.lire_article, name='blog_lire'),
  ]

urls.py:

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings

urlpatterns = [
  url(r'^blog/', include('blog.urls')),
  url(r'^admin/', include(admin.site.urls)),
  ]

accueil.html:

{% extends "blog/base.html" %}
{% block title %}Accueil du blog{% endblock %}
{% block content %}
  <h1>Derniers articles</h1>

  {% for article in articles %}
    <h2 class="titre-article">{{ article.titre }}</h2>

    <p class="infos-article">Le {{ article.date|date:'SHORT_DATETIME_FORMAT' }}, XX commentaires.</p>

    <div class="contenu-article">
        {{ article.contenu|safe|truncatewords_html:30 }}
    </div>

    <p><a href="{% url 'blog_lire' article.slug %}">Lire la suite</a></p>

    <hr />
  {% endfor %}
{% endblock %}

base.html文件:

{% extends "blog/base.html" %}

{% block title %}{{ article.titre }}{% endblock %}

{% block content %}

  <p>
      <a href="{% url 'accueil' %}">Retour à la liste des articles</a>
  </p>

  <h2 class="titre-article">{{ article.titre }}</h2>
  <p class="infos-article">Par {{ article.auteur }}, le {{ article.date }}</p>

  <div class="contenu-article">
    {{ article.contenu|safe }}
  </div>

  <h3 class="titre-comments">Commentaires</h3>

  <h4>Poster votre propre commentaire</h4>
  <form method="post" action="" class="pure-form">

     <!-- A vous de remplir le formulaire également -->

     <p style="text-align:center">
         <input class="pure-button pure-button-primary" type="submit" />
     </p>
   </form>

   <div class="clear"></div>


   <div class="commentaire">
     <p>Maxime, le 28 mai 2019</p>
     <p>Ceci est un exemple de commentaire, qui est à remplacer par votre modèle</p>
   </div>
 {% endblock %}

2 个答案:

答案 0 :(得分:1)

至少有一篇文章有​​一个空的slu ..

错误显示它正在尝试使用slug找到链接到每篇文章的“blog_lire”页面的正确URL。但它失败了,因为它有一个空字符串,因此它不能生成该链接。

你应该确保你的所有文章都有一个slug字段的值。

答案 1 :(得分:-1)

而不是

{% url 'blog_lire' article.slug %}"

尝试

{% url 'blog:blog_lire' slug=article.slug %}"

或类似的东西