简体   繁体   English

不能让django网址工作

[英]Can't get django urls to work

I've got a simple django setup in which I have one app called ' lists '. 我有一个简单的django设置,其中有一个名为' lists '的应用程序。 I now want http://127.0.0.1:8000/lists to show this app. 我现在想要http://127.0.0.1:8000/lists来显示这个应用程序。 So I changed my main urls.py to the following: 所以我将主urls.py更改为以下内容:

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: 我将位于lists-folder(我的app称为列表)的urls.py更改为以下内容:

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: 据我所知,我完全按照django教程中的说明操作,但是当我访问http://127.0.0.1:8000/lists没有斜杠)时,它给出了以下错误:

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: 当我访问http://127.0.0.1:8000/lists/带有斜杠)时,它给出了以下错误:

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. 我不明白为什么当我使用尾部斜杠访问url时它不再搜索^ lists /。 Does anybody know what I am doing wrong here? 有谁知道我在做错了什么?

All tips are welcome! 欢迎所有提示!

You're missing the empty string at the start of your patterns in lists urls.py . 您在列表urls.py中的patterns开头缺少空字符串。

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. 空白字符串是一个视图前缀 ,可用于协助DRY主体。 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

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'),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM