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:
我有一个简单的django设置,其中有一个名为'lists'的应用程序。我现在想要http://127.0.0.1:8000/lists来显示这个应用程序。所以我将主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. Does anybody know what I am doing wrong here?
我不明白为什么当我使用尾部斜杠访问url时它不再搜索^ lists /。有谁知道我在做错了什么?
All tips are welcome!
欢迎所有提示!
1 个解决方案
#1
11
You're missing the empty string at the start of your patterns
in lists urls.py
.
您在列表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.
空白字符串是一个视图前缀,可用于协助DRY主体。它用于为视图路径添加前缀。
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'),
)
#1
11
You're missing the empty string at the start of your patterns
in lists urls.py
.
您在列表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.
空白字符串是一个视图前缀,可用于协助DRY主体。它用于为视图路径添加前缀。
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'),
)