13

詳細ビューを表示するようにURLを構成するのに問題があります。このリンクをクリックすると、が表示されると思ったときに、が表示され<a href='{% url blog_detail blog.slug %}'>{{ blog.name }}</a>ます。エラーはなく、ブラウザバーには次のように表示されます。それでも、からのhtmlが表示されます。なぜ何かアイデアはありますか?あなたのアイデアをありがとう。blog.htmlblog-detail.htmlexample.com/blog/the-slugblog.htmlblog-detail.html

url:

url(r'^blog/', 'myapp.views.blog', name='blog'),
url(r'^blog/(?P<slug>[\w-]+)/$', 'myapp.views.blog_detail', name='blog_detail'),

ビュー:

def blog(request):
    blog_list = Blog.objects.all()
    return render(request, 'blog.html', {'blog_list':blog_list})

def blog_detail(request, slug):
    blog = get_object_or_404(Blog, slug=slug)
    return render(request, 'blog-detail.html', {'blog':blog})

編集:@omouseによって要求された出力

これは、リンクをクリックしたときの出力です。とまったく同じですblog.htmlが、である必要がありますblog-detail.html

<div id='content-wrapper'>
<section>
<div class='blog-name'><h2><a href='/blog/test/'>Test</a></h2></div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a ...

<div class='blog-name'><h2><a href='/blog/second-test/'>Second Test</a></h2></div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a ...
</section>
</div>
4

2 に答える 2

28

URLが問題です。最初のURLはすべて(、、/blog//blog/test/に一致します。一致するのは、末尾に/blog/awdlawdjaawldドル記号が必要です。$/blog/

url(r'^blog/$', 'myapp.views.blog', name='blog'),
url(r'^blog/(?P<slug>[\w-]+)/$', 'myapp.views.blog_detail', name='blog_detail'),

上記は正しく機能するはずです。

これは正規表現の良いリファレンスです

于 2013-02-26T02:41:35.530 に答える
0

ルドルフは絶対に正しい

停止したブログは/$、slugによって呼び出されたすべてのサブページをキャッチできないため、サブページがある場合は/$、次のようにフォルダーレベルに追加する必要があります。

re_path('brands/$', AllBrands.as_view(), name="brands"),
re_path(r'^brands/(?P<slug>[\w-]+)/$', BrandDetail.as_view(), name = 'brandetail'),

これはdjango2.2です

アフターブランドがなければ/$、スラッグページにはブランドリストページが表示されていました。

于 2019-11-08T23:32:24.267 に答える