1

I'd like to run two apps with the same url patterns. I would like to avoid having an app-specific slug like domain.com/pages/something-here or domain.com/blog/something-there.

I tried this:

# urls.py
urlpatterns = patterns('',
    url(r'^$', 'my.homepage.view'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('pages.urls')),
    url(r'^', include('blog.urls')),
)

# pages/urls.py
urlpatterns = patterns('',
    url(r'^(.+)/$', views.page),
)

# blog/urls.py
urlpatterns = patterns('',
    url(r'^(.+)/$', views.post),
)

My code doesn't work, whichever include comes first (here, pages.urls) works ok, other urls (for blog) throw 404.

Thanks in advance

EDIT: I did it like this: created glue.py in the same directory as settings.py. It will handle my homepage and this dispatcher view:

def dispatcher(request, slug):
    try:
        page = get_object_or_404(Page, slug=slug)
        return render(request, 'pages/page.html', {'page': page})
    except:
        post = get_object_or_404(Post, slug=slug)
        return render(request, 'blog/post.html', {'post': post})

I don't know if it's ok. I hope there is a better way.

Thanks for the comments.

3 Answers 3

1

I don't know if this is a better answer. But, if these situations are satisfied for you..

  1. if your django app is based on django template rendering.

  2. The url you are talking about, need not be accessed directly by typing the endpoint in the browser itself.

Then, maybe you could consider url namespaces and template redirections.

https://docs.djangoproject.com/en/1.11/topics/http/urls/#url-namespaces

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. Quite some time passed since I asked for it though. By now I don't even really know what is it exactly about :) In fact I don't think Django is a good framework anymore and I don't plan to get back to it ever again. But again, thank you!
Always a pleasure
0

This doesn't work because django urls are resolved in order, meaning that the first url that matches the regexp will be the resolved one. In your case, the the urls included from the blogs application will never be searched, as django already resolved the url on the pages includes line.

Also, the django url module is not supposed to know if a certain page or blog post exists, as i believe in your application this is determined with a database lookup.

The urls module just executes the view that is connected to the first regexp that matches.

You should change your logic, e.g. with perpending "blog/" to blog urls (what's wrong with that?)

   url(r'^blog/', include('blog.urls')),
   url(r'^', include('pages.urls')),

Notice that the i moved the blog url up, as most generic regxexp should always be the last to be tried by django url resolver.

Alternatively, you could code a proxy view that tries both blog posts and pages. but it doesn't seem the best way to do it to me.

Comments

-1

How would you like this to work? They're both using the same URL (which of course is causing problems). How would a user get to a "page" rather than a "blog" or vice versa?

In general, you can't have overlapping URLs in your URL patterns (without including additional data).

EDIT:

So you want the first app to check if it has a view to match the URL and next to take over if the first doesn't? You could do something complicated like writing a "view matcher" to do want you want, but there are much more straigtforward solutions.

The easiest way would be to alter the slug generation function for one of your apps. Have one use some delimeter other than underscores, or always append the name of the app to the slug. This way you could find pages because their url would be "some-slug-page" and blogs would be "some-slug-blog", which you could then write a URL pattern for. If you don't want to add the entire URL, you can append/prepend just the first letter, or whatever you want.

Just think about a way that's acceptable to you to generate URLs for each app which, just by reading the URL, lets you know which app the page belongs to.

4 Comments

let's say I have a page with a slug 'about'. The url for it would be: domain.com/about-us and there are blogposts eg.: domain.com/blog-post-one domain.com/blog-post-two I'd like django to check if there is a match in pages first (because there is less to check) and than proceed to blogposts
I guess I can have a master view catching this url pattern and dispatching either pages.view.page or blog.view.post --- but it doesn't seem right for some reason
Thanks, the case is that I don't want urls to be distinguishable between two (or more) apps. So far I took the "view matcher" approach (see my op EDIT). BTW, great blogging from you, I enjoy a lot, thanks!
-1. That's not the problem as you can see it works in stackoverflow.com/a/11618280/1402286

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.