s_spirit
s_spirit

Reputation: 115

Two apps in one URL in Django

I try to link two apps to one URL. I found an example, where that can be possible with "include" command and two separate "urls.py" for each app.

So, I tried to do that, but it still only one app works on page... Please, help.

My main URL config:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
 # Examples:
 url(r'^admin/', include(admin.site.urls)),
 url(r'^', include('joins.urls')),
 url(r'^', include('item.urls')),
 url(r'^items/get/(?P<item_id>\d+)$', 'item.views.item', name='item'),
 url(r'^(?P<ref_id>.*)$', 'joins.views.share', name='share'),
)

My first app URL config:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
 # Examples:
 url(r'^', 'joins.views.home', name='home'),
)

My second app URL config:

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
 # Examples:
 url(r'^', 'item.views.items', name='home'),
)

Upvotes: 0

Views: 1793

Answers (3)

joel goldstick
joel goldstick

Reputation: 4493

You want something like this I think:

 url(r'^joins/', include('joins.urls')),
 url(r'^items/', include('item.urls')),

see this: https://docs.djangoproject.com/en/1.7/topics/http/urls/#including-other-urlconfs

@s_spirit You want two views for one URL? To do that write a view that gets both models, pass both to your render function and make a template that displays what you want from each. Making two urls the same will only do what the first match does – joel goldstick 2 hours ago

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599600

Your entire approach is mistaken, unfortunately. A view is responsible entirely for responding to a URL and returning a response. It simply doesn't make sense to talk about having two views at a single URL.

If you need functionality provided by two apps within one URL, then think about abstracting the shared functionality into a utility method, or a template tag, or a context processor.

Upvotes: 1

catavaran
catavaran

Reputation: 45575

Add the dollar sign to the regex:

url(r'^$', 'joins.views.home', name='home'),

Without the $ the ^ regex matches all urls.

Of course the item.views.items will not work anyway (django executes the first matched url) but other views from item app will work just fine.

Upvotes: 0

Related Questions