Philipp S.
Philipp S.

Reputation: 981

How to handle url collisions in django/wagtail

how to handle two different views witch match for the same url? (Both views need a database call to determine if the called element is available. Changing the url structure is not an option.)

url(r'^', include(wagtail_urls)),

This wagtail url is matching all url's and is raising an 404 if the according page/slug is not in the database.

But, I also have my own view witch behaves similar. How can I tell django to continue with the next url instead of raising a 404?

(I could place my own view before the wagtail view and modify it, but I don't know how to return to the next url?)

This is my solution at the end:

from wagtail.wagtailcore.views import serve

    # ... in my view where I normally return a 404 => I set this:
    return serve(self.request, self.request.path)

Upvotes: 1

Views: 404

Answers (1)

Youngil Cho
Youngil Cho

Reputation: 159

First of all, Sharing same url pattern in diffrent views is bad idea. The bigger your project, the harder it will be to maintain it.

Nevertheless, there is a way if you want that way.

You can place your own view first in urls.py like your saying,

process some your own logic first and catch 404 exception when there is nothing to show in your view than simply call the "Wagtail" view with request original parameters(page, slug, etc..) with return statement.

Below is example. This example is based on Django functional based view, but there is the way in class based view something like this.

def your_own_view(request, slugs):
    try:
        article = get_object_or_404(Article, slugs=slugs)
    except Http404:
        return some_wagtail_view(request, slugs)

    ...

    return render(request, "article/view.html", context)

Upvotes: 3

Related Questions