Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get django urls to work

I've got a simple django setup in which I have one app called 'lists'. I now want http://127.0.0.1:8000/lists to show this app. So I changed my main urls.py to the following:

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

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^lists/', include('lists.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

and I changed the urls.py which resides in the lists-folder (my app is called lists) to the following:

from django.conf.urls import patterns, url
from lists import views

urlpatterns = patterns(
    url(r'^$', views.index, name='index')
)

As far as I know I am following the instructions in the django tutorial perfectly well, but when I visit http://127.0.0.1:8000/lists (without trailing slash) it gives me the following error:

Page not found (404) Request Method:    GET Request URL:    http://127.0.0.1:8000/lists

Using the URLconf defined in companyLists.urls, Django tried these URL patterns, in this order:

    ^lists/
    ^admin/

The current URL, lists, didn't match any of these.

and when I visit http://127.0.0.1:8000/lists/ (with a trailing slash) it gives me the following error:

Page not found (404) Request Method:    GET Request URL:    http://127.0.0.1:8000/lists/

Using the URLconf defined in companyLists.urls, Django tried these URL patterns, in this order:

    ^admin/

The current URL, lists/, didn't match any of these.

I don't understand why it doesn't search for ^lists/ anymore when I visit the url with the trailing slash. Does anybody know what I am doing wrong here?

All tips are welcome!

like image 235
kramer65 Avatar asked Dec 07 '25 10:12

kramer65


1 Answers

You're missing the empty string at the start of your patterns in lists urls.py.

Try this:

urlpatterns = patterns('',
    url(r'^$', views.index, name='index')
)

The blank string is a view prefix that you can use to assist with the DRY principal. It is used to prefix your views path.

For example, (extending your example above):

Rather than having:

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^homepage$', views.homepage, name='index'),
    url(r'^lists$', views.lists, name='index'),
    url(r'^detail$', views.detail, name='index'),
)

You can use:

urlpatterns = patterns('views',
    url(r'^$', index, name='index'),
    url(r'^homepage$', homepage, name='index'),
    url(r'^lists$', lists, name='index'),
    url(r'^detail$', detail, name='index'),
)

To have multiple view prefixes just segment your urlpatterns.

urlpatterns = patterns('views',
    url(r'^$', index, name='index'),
    url(r'^homepage$', homepage, name='index'),
    url(r'^lists$', lists, name='index'),
    url(r'^detail$', detail, name='index'),
)

urlpatterns += patterns('more_views',
    url(r'^extra_page$', extra_page, name='index'),
    url(r'^more_stuff$', something_else, name='index'),
)
like image 107
Ewan Avatar answered Dec 10 '25 01:12

Ewan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!