当我将url添加到另一个应用程序时出现错误。 我添加了
<a class="navbar-brand" href="{% url 'posts:listofposts' %}">Home</a>
我的root.urls:
urlpatterns = [
url(r'^', include('posts.urls', namespace= 'posts'))
]
posts.urls:
urlpatterns = [
url(r'^create/',
create_post ,
name='create_post'),
url(r'^(?P<slug>[-\w]+)/edit/$',
update_post,
name = 'update_post'),
url(r'^category/(?P<slug>[-\w]+)/$',
category,
name='category'),
url(r'^(?P<slug>[-\w]+)/$',
detail,
name = 'detail'),
url(r'^$',
listofposts ,
name='listofposts'),
]
和我在post.views中的观点:
def listofposts(request, category_slug = None):
html = 'base.html'
Category_all = Category.objects.all()
query_set_list = Post.objects.all()
query = request.GET.get("q")
if query:
query_set_list = query_set_list.filter(title__icontains=query)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = Post.filter(category=category)
context = {
"title" : 'Записки',
"list" : query_set_list,
'category_all' : Category_all,
}
return render(request, html, context)
def detail(request, slug):
Category_all = Category.objects.all()
html = 'post_detail.html'
query_set = Post.objects.all()
instance = get_object_or_404(Post, slug = slug)
context = {
"title" : instance.title,
"instance" : instance,
"list": query_set,
'category_all' : Category_all,
}
return render(request, html, context)
我得到了下一个错误
Exception Value:
Reverse for 'detail' not found. 'detail' is not a valid view function or pattern name.
<a href="{{ x.get_absolute_url }}"><p>{{ x.title }}</p></a>
一切都很好,直到我添加了url链接到另一个应用程序。如果我删除url链接到另一个视图everithing将没关系。如果你帮忙,那就太多了。
答案 0 :(得分:2)
在Post
模型的get_absolute_url
方法中反转网址时,您需要包含命名空间:
reverse('posts:detail', args=[str(self.slug)])