2017-04-26 19 views
0

我正在關注「django by example」 ,我遇到了這個問題,但我不知道是什麼原因造成的。NoReverseMatch at/blog/

以下是錯誤頁面:在/博客


NoReverseMatch/ 反向的 'post_detail' 與參數 '(' ' '', '')' 未找到。嘗試1個模式:['blog /(?P \ d {4})/(?P \ d {2})/(?P \ d {2})/(?P [ - \ w] + )/ $']

請求方法: GET

請求URL: http://127.0.0.1:8000/blog/

Django的版本: 1.11

異常類型: NoReverseMatch

異常值: 反向對於'post_detail' '找不到'('','','')'。嘗試1個模式:['blog /(?P \ d {4})/(?P \ d {2})/(?P \ d {2})/(?P [ - \ w] + )/ $']

異常地點: E:\工作區\ pycharm \ djangobyexample \ mysite的\ ENV \ LIB \站點包\ Django的\網址\ resolvers.py在_reverse_with_prefix,線497

Python的可執行文件: E:\工作空間\ pycharm \ djangobyexample \ mysite的\ ENV \腳本\ python.exe

Python的版本: 3.5.2

Python的路徑: ['E:\工作空間\ pycharm \ djangobyexample \我的網站' , 'E:\ workspace \ pycharm \ djangobyexample \ mysite \ env \ Scripts \ python35.zip', 'E:\ workspace \ pycharm \ djangobyexample \ mysite \ env \ DLLs', 'E:\ workspace \ pycharm \ djangobyexample \ mysite \ env \ lib', 'E:\ workspace \ pycharm \ djangobyexample \ mysite \ env \ Scripts', 'c:\ users \ richard \ appdata \ local \ programs \ python \ python35 \ Lib', 'c:\ users \ richard \ appdata \ local \ programs \ python \ python35 \ DLLs', 'E:\ workspace \ pycharm \ djangobyexample \ mysite \ env', 'E:\ workspace \ pycharm \ djangobyexample \ mysite \ env \ lib \ site-packages']



主要URLConfiguration

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

博客/ url.py

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

urlpatterns = [ 
    # post views 
    # url(r'^$', views.post_list, name='post_list'), 
    url(r'^$', views.PostListView.as_view(), name='post_list'), 
    url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$', 
     views.post_detail, 
     name='post_detail'), 
    #url(r'^(?P<post_id>\d+)/share/$', views.post_share, name='post_share'), 
] 

views.py

from django.shortcuts import render, get_object_or_404 
from .models import Post 
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 
from django.views.generic import ListView 
from .forms import EmailPostForm 
from django.core.mail import send_mail 


# Create your views here. 

class PostListView(ListView): 
    queryset = Post.published.all() 
    context_object_name = 'posts' 
    paginate_by = 3 
    template_name = 'blog/post/list.html' 



def post_detail(request, year, month, day, post): 
    post = get_object_or_404(Post, slug=post, 
          status='published', 
          publish__year=year, 
          publish__month=month, 
          publish__day=day) 
    return render(request, 'blog/post/detail.html', {'post': post}) 

models.py

from django.db import models 
from django.utils import timezone 
from django.contrib.auth.models import User 
from django.core.urlresolvers import reverse 


class PublishedManager(models.Manager): 
    def get_query(self): 
     return super(PublishedManager, self).get_query().filter(status='published') 


class Post(models.Model): 
    STATUS_CHOICES = { 
     ('draft', 'Draft'), 
     ('published', 'Published'), 
    } 
    title = models.CharField(max_length=250, primary_key=True) 
    slug = models.SlugField(max_length=250, unique_for_date='publish') 
    author = models.ForeignKey(User, related_name='blog_post') 
    body = models.TextField() 
    publish = models.DateTimeField(default=timezone.now) 
    created = models.DateTimeField(auto_now_add=True) 
    updated = models.DateTimeField(auto_now=True) 
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft') 

    class Meta: 
     # Telling django to sort results by the publish field in descending order by default when we query the database 
     ordering = ('-publish',) 

    def __str__(self): 
     return self.title 

    objects = models.Manager() 
    published = PublishedManager() 

    def get_absolute_url(self): 
     return reverse('blog:post_detail', args=[self.publish.year, 
               self.publish.strftime('%m'), 
               self.publish.strftime('%d'), 
               self.slug]) 

細節。HTML

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

{% block title %}{{ post.title }}{% endblock %} 

{% block content %} 
    <h1>{{ post.title }}</h1> 
    <p class="date"> 
     Published {{ post.publish }} by {{ post.author }} 
    </p> 
    {{ post.body|linebreaks }} 

{% endblock %} 

list.html

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

{% block title %}My Blog{% endblock %} 

{% block content %} 
    <h1>My Blog</h1> 
    {% for post in posts %} 
     <h2> 

      <a href="{{ post.get_absolute_url }}"> 
       {{ post.title }} 
      </a> 
     </h2> 
     <p class="date"> 
      Published {{ post.publish }} by {{ post.author }} 
     </p> 
     {{ post.body|truncatewords:30|linebreaks }} 
    {% endfor %} 
    {% include "pagination.html " with page=page_obj %} 
{% endblock %} 

base.html文件

{% load staticfiles %} 

<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>{% block title %}{% endblock %}</title> 

</head> 
<body> 
    <div id="content"> 
     {% block content %} 
     {% endblock %} 
     </div> 
     <div id="sidebar"> 
      <h2>My blog</h2> 
      <p>This is my blog.</p> 
     </div> 
</body> 
</html> 

回答

2

此行是給你的錯誤,因爲這些論點是無效的。

<!-- <a href="{% url 'blog:post_detail' post.year post.month post.day %}">--> 

的文章沒有yearmonthday屬性 - 它們是post.publish屬性。

您已在模板的下一行使用{{ post.get_absolute_url }}來獲取網址。由於url標記行位於html註釋<!-- -->內,因此最簡單的解決方法是簡單地刪除該行。

+0

感謝您的答覆,我已經嘗試過但它不起作用。 – Richard

+0

如果你刪除那一行,那麼你不應該得到那個錯誤,除非你在你的模板中的其他地方有'{%url'博客:post_detail'...%}',你沒有向我們顯示。 – Alasdair

+0

謝謝你,我再次檢查了我的代碼,沒有任何其他使用{%url'的博客:post_detail'...%}或「xx.get_absolute_url」。我現在應該怎麼做...... – Richard