开发者

Django - How to use URLconfs with an apps folder?

开发者 https://www.devze.com 2023-02-01 14:15 出处:网络
I\'m following the tutorial on the Django website but I\'m trying to expand upon it.I like the organizational scheme of putting all of your apps in an \"apps\" folder.I\'m trying to figure out the pro

I'm following the tutorial on the Django website but I'm trying to expand upon it. I like the organizational scheme of putting all of your apps in an "apps" folder. I'm trying to figure out the proper way to include urls.py in order to get everything to link together.

Here's my root urls.py:

from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^polls/', include('apps.polls.urls')),
    (r'^admin/', include(admin.site.urls)),
)

Here's my urls.py at apps/polls/urls.py:

from django.conf.urls.defaults import *

urlpatterns=patterns('polls.views',
    (r'^polls/$', 'index'),
    (r'^polls/(?P<poll_id>\d+)/$', 'detail'),
    (r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
    (r'^polls/(?P<poll_id>\d+)/vo开发者_运维知识库te/$', 'vote'),
)

What's the correct way to do this? Thanks!


The way you currently have it set up... the URLs for polls would be:

http://your.url.here/polls/polls/235/results/

This is probably not what you want. The include function in the urlpatterns in the root urls.py file specifies "polls/" as a prefix to all urlpatterns in the polls app. Therefore, in the polls/urls.py file, you shouldn't specify the "polls/" prefix again as it will cause duplicate prefixes.


How are you running your Django instances? If you have multiple vhosts configured in Apache then each Django instance in /apps has it's own urls.py.


I got it to work by doing this:

urlpatterns=patterns('polls.views',
    (r'^$', 'index'),
    (r'^(?P<poll_id>\d+)/$', 'detail'),
    (r'^(?P<poll_id>\d+)/results/$', 'results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

I guess the polls part is taken care of in the root urlconf

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号