2017-02-15 382 views
0

私はDjangoフレームワークを学んでいますが、テンプレートの一部を拡張しています。私のブログの投稿のタイトルをクリックすると新しいテンプレートを追加しようとしています私はviews.pyの新しいビューを作成し、のURLに新しいURLを作成します。、しかし、私がのURLのパスをadcすると、 HREF」私はページをリロードしたときに表示されるように、.htmlのファイルの望ましいが、私は次のエラーが表示されます。Djangoエラー:テンプレートレンダリング中にエラーが発生しました

NoReverseMatch at /

Reverse for 'blog.views.post_detail' with arguments '()' and keyword arguments '{'pk': 2}' not found. 0 pattern(s) tried: []

そして

Error during template rendering

In template /home/douglas/Documentos/Django/my-first-blog/blog/templates/blog/post_list.html, error at line 9

だから私はすべてがうまく機能デフォルト値をバックアップするHREFを消去する際に、...私は何かがのhrefラインに間違っていることはほぼ確信しているが、私は、すべてのテンプレート関連のファイルを掲載しますあなたがチェックするためにあなたがチェックする必要がある場合は、何かが私に教えて:

もみhtmlファイル:post_list.htmlを

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

{% block content %} 
    {% for post in posts %} 
     <div class="post"> 
      <div class="date"> 
       {{ post.published_date }} 
      </div> 
      <h1><a href="{% url 'blog.views.post_detail' pk=post.pk %}">{{ post.title }}</a></h1> 
      <p>{{ post.text|linebreaksbr }}</p> 
     </div> 
    {% endfor %} 
{% endblock content %} 

urls.py:

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

urlpatterns = [ 
    url(r'^$', views.post_list), 
    url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail), 
] 

views.py

from django.shortcuts import render 
from django.shortcuts import render, get_object_or_404 
from .models import Post 
from django.utils import timezone 

def post_list(request): 
    #posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') 
    posts = Post.objects.all() 
    return render(request, 'blog/post_list.html', {'posts': posts}) 

def post_detail(request, pk): 
    post = get_object_or_404(Post, pk=pk) 
    return render(request, 'blog/post_detail.html', {'post': post}) 

まあみんな、私は私の質問についての詳細を忘れていないと思っている、ということだと思います... ありがとうございました、どんな助けも歓迎です!!

答えて

1

あなたのURLの名前を定義する必要があります。それは良いです。このよう

urlpatterns = [ 
    url(r'^$', views.post_list, name='post_list'), 
    url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'), 
] 

、あなたのテンプレートでは、あなたではurlタグ

<h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1> 

まず情報(名前とURLに対して定義)

The first argument is a url() name. It can be a quoted literal or any other context variable.

第二の情報(にこの名前を使用することができますケース、名前のないURL)

If you’d like to retrieve a namespaced URL, specify the fully qualified name:

{% url 'myapp:view-name' %}

More information here

+0

感謝の男、私の仕事!どうもありがとうございました!! –

関連する問題