python - Multiple django apps using same url pattern -
i'd run 2 apps same url patterns. avoid having app-specific slug 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: did this: created glue.py in same directory settings.py. handle homepage , dispatcher view:
def dispatcher(request, slug): try: page = get_object_or_404(page, slug=slug) homecoming render(request, 'pages/page.html', {'page': page}) except: post = get_object_or_404(post, slug=slug) homecoming render(request, 'blog/post.html', {'post': post})
i don't know if it's ok. hope there improve way.
thanks comments.
this doesn't work because django urls sesolved in order, meaning first url matches regexp resolved one. in case, the urls included blogs application never searched, django resolved url on pages includes line.
also, django url module not supposed know if page or blog post exists, believe in application determined database lookup.
the urls module executes view connected first regexp matches.
you should alter logic, e.g. prepending "blog/" blog urls (what's wrong that?)
url(r'^blog/', include('blog.urls')), url(r'^', include('pages.urls')),
notice moved blog url up, generic regxexp should lastly tried django url resolver.
alternatively, code proxy view tries both blog posts , pages. doesn't seem best way me.
python django django-urls
No comments:
Post a Comment