使用在SchoolnSkill.urls中定义的URLconf,Django按以下顺序尝试了这些URL模式:

时间:2017-10-31 15:00:55

标签: python django

好的,这是Django人们面临的一个非常普遍的问题,互联网上已有几篇文章可用,但没有一篇能帮助我。我明白我需要在“apps / url”中创建自己的URLPatterns,并且我创建了一个。以下是我的项目“schoolnskill / url”中的URLPatterns:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^user_info/', include('user_info.urls', namespace='user_info')),

]

以下是我的应用程序user_info / url

中的网址格式
from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^user_info/index/$', views.IndexView.as_view(), name='index'),
    url(r'^user_info/register/$', views.RegisterView.as_view(), name='sign_up'),
    url(r'^register/$', views.UserFormView.as_view(), name='register'),

]

我收到“user_info / index”错误,即使我已将其添加到我的网址模式中。

以下是整个错误堆栈:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/user_info/index/
Using the URLconf defined in SchoolnSkill.urls, Django tried these URL patterns, in this order:
^admin/
^user_info/ ^user_info/index/$ [name='index']
^user_info/ ^user_info/register/$ [name='sign_up']
^user_info/ ^register/$ [name='register']
The current path, user_info/index/, didn't match any of these

我的Python环境详细信息:

Python - 3.6
Django - 1.11.3
IDE - Spyder 3.2.4

删除'user_info'后出现新错误

Exception Type: ImproperlyConfigured
Exception Value:    
IndexView is missing a QuerySet. Define IndexView.model, IndexView.queryset, or override IndexView.get_queryset()

2 个答案:

答案 0 :(得分:0)

在根网址配置中使用user_info/时,您已经拥有include()

因此,您应将其从user_info/urls.py删除。

urlpatterns = [
    url(r'^index/$', views.IndexView.as_view(), name='index'),
    url(r'^register/$', views.RegisterView.as_view(), name='sign_up'),
    # this url pattern now clashes with the one above, so you need to change it if you need both
    url(r'^register/$', views.UserFormView.as_view(), name='register'),
]  

答案 1 :(得分:0)

可能是因为您没有从user_info app导入urlpatter?

在你的schoolnskill urls.py文件的顶部写

from project_name.user_info import urlpatterns

然后在你的url include语句写:

url(r'^user_info/', include(urlpatterns))