我在尝试访问index.html模板中的帖子时遇到此错误:
反向' single_post'参数'(5,)'和关键字参数 ' {}'未找到。尝试了1种模式:[' $(?P [0-9] +)/ $']
index.html模板:
{% if hot_posts %}
<ul>
{% for post in hot_posts %}
{% if post.acceptable %}
<li>
<h2>
<a href="{% url 'post:single_post' post.id %}/">{{post.title}}</a>
</h2>
<a href="{% url 'post:single_post' post.id %}/">
<img src="{{ post.image.url }}" alt="{{post.title}}"
class="post-img" />
</a>
<p>امتیاز: <span>{{ post.likes }}</span></p>
</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>No posts are avaialable.</p>
{% endif %}
post.views:
from django.shortcuts import render, get_object_or_404
from .models import Post
# Create your views here.
def index(request):
hot_posts = Post.objects.order_by('-likes')[:10]
template = 'posts/index.html'
context = {"hot_posts":hot_posts}
return render(request, template, context)
def single_post(request, post_id):
post = get_object_or_404(Post, pk=post_id)
template = 'posts/single_post.html'
context = {'post': post}
return render(request, template, context)
project urls.py:
from django.conf.urls import include, url
from post.admin import site_admin
urlpatterns = [
url(r'^modir/', include(site_admin.urls)),
url(r'^$', include('post.urls', namespace='post')),
]
app urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name="home_page"),
url(r'^mosama/(?P<post_id>[0-9]+)/$', views.single_post,
name="single_post")
]
答案 0 :(得分:3)
从包含帖子网址的正则表达式中删除$
。
url(r'^', include('post.urls', namespace='post')),