"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 \個人用サイト\ envを\ libに\のsite-packages \ジャンゴ\のURL \ resolvers.pyで_reverse_with_prefix、ライン497
Pythonの実行可能ファイル: E:\ワークスペース\ pycharm \ djangobyexample \個人用サイト\のenv \スクリプト\ python.exe
Pythonのバージョン:3.5.2
Pythonのパス: [ 'E:\ワークスペース\ pycharm \ djangobyexample \私のサイト' 、 'E:\ワークスペース\ pycharm \ djangobyexample \個人用サイト\のenv \スクリプト\ python35.zip'、 'E:\ワークスペース\ pycharm \ djangobyexample \個人用サイト\ envを\のDLL'、 「E:\ワークスペース\ pycharm \ 'E:\ workspace \ pycharm \ djangobyexample \ mysite \ env \ Scripts'、 'c:\ users \ richard \ appdata \ local \ programs \ python \ python35 \ Lib'、 'E:¥workspace¥pycharm¥djangobyexample¥mysite¥env'、 'E:¥workspace¥pycharm¥djangobyexample¥mysite¥c:¥users¥richard¥appdata¥local¥programs¥python¥python35¥DLLs 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 %}
{% 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 %}
するlist.html 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>
返信いただきありがとうございます、私はalreayを試みましたが、動作しません。 – Richard
あなたが私たちに表示されていないテンプレートのどこかに '{%url 'blog:post_detail' ...%}'がないかぎり、その行を削除するとエラーは発生しません。 – Alasdair
ありがとう、私は私のコードをもう一度チェックして、{%url 'blog:post_detail' ...%}や "xx.get_absolute_url"の使用はありません。どうすればいいですか? – Richard