0

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'),
)
s_spirit
  • 111
  • 10

3 Answers3

1

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

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.

catavaran
  • 44,703
  • 8
  • 98
  • 85
  • Thx, but I dont understand, what means "other views". I have two apps. One of them (joins) - subscribe form, another (item) - list of blogs. All, what i want to do - see them both at one page... So, how I can do that? – s_spirit Mar 23 '15 at 11:41
  • My view for 'item': from django.shortcuts import render from django.http.response import HttpResponse # Create your views here. def items(request): return render_to_response('items.html', {'items': Item.objects.all()}) def item(request, item_id): return render_to_response('item.html', {'item': Item.objects.get(id=item_id), 'comments': Comments.objects.filter(comments_item_id=item_id)}) – s_spirit Mar 23 '15 at 11:44
  • You can't have two views at the same url. Django runs the first view for the matched url. – catavaran Mar 23 '15 at 11:48
  • @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 Mar 23 '15 at 13:17
0

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

joel goldstick
  • 4,393
  • 6
  • 30
  • 46