0

Mistake appeared when I added url to another app. I added

<a class="navbar-brand" href="{% url 'posts:listofposts' %}">Home</a> 

My 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'),
]

and my view in 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)

And I got the next mistake

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>

Everything worked fine untill I added url link to another app. If I delete url link to another view everithing will be okay. Thx a lot if you help.

1 Answers1

3

You need to include the namespace when you reverse the URL in your Post model's get_absolute_url method:

reverse('posts:detail', args=[str(self.slug)])
souldeux
  • 3,615
  • 3
  • 23
  • 35
Alasdair
  • 298,606
  • 55
  • 578
  • 516