Django“否模块命名URL”错误 [英] Django "No Module Named URLs" error

查看:162
本文介绍了Django“否模块命名URL”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经有很多类似的问题发布了,但是我已经尝试过这些解决方案。我正在通过一个基本的Django教程,这里是我的代码:



urls.py



<$ p $来自django.conf.urls导入模式的p> 包含来自django.contrib的url

import admin
admin.autodiscover()

urlpatterns = patterns('',
#示例:
#url(r'^ $','tango_with_django_project.views.home',name ='home'),
#url r'^ tango_with_django_project /',include('tango_with_django_project.foo.urls')),

#取消注释下面的admin / doc行以启用管理员文档:
url(r'^ admin / doc /',include('django.contrib.admindocs.urls')),

#取消注释下一行以启用admin:
url(r'^ admin /', include(admin.site.urls)),

url(r'^ rango /',include('rango.urls')),#添加这个新的TUPLE!

views.py

  from django.http import HttpResponse 

def index(re任务):
返回HttpResponse(Rango说你好世界!)

从设置.py文件

  ROOT_URLCONF ='tango_with_django_project.urls'

希望你们都能帮助我开始

解决方案

一个名为FailBook的Django项目,包含两个应用程序,帖子和链接。如果我看看FailBook / urls.py,我会从django.conf.urls导入模式中找到类似于



$ $ $ $ $ $ $ $ $ url

from django.contrib import admin
admin.autodiscover()

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

url(r'^ posts /',include('posts.urls')),##自定义URL包括
url r'^ links /',include('links.urls'))##自定义URL包括

所以,当你查看目录结构时,你会注意到有两个urls.py文件

  FailBook 
| - 帖子
| - models.py
| - urls.py
| - views.py
| - etc
| - links
| - models.py
| - urls.py
| - views.py
| - 等

#urls.py文件在文件夹
从django.conf.urls导入模式,包括,url
从.views导入PostListView,PostDetailV iew

urlpatterns = patterns('',

url(r'^ posts /',PostListView.as_view()),
url(r'^ posts /(?P< post_id> \d +)',PostDetailView.as_view()),

#其中两个视图都是基于类的视图,因此as_view函数调用


There are many similar questions posted already, but I've already tried those solutions to no avail. I'm working through a basic Django tutorial, and here is my code:

urls.py

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

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'tango_with_django_project.views.home', name='home'),
    # url(r'^tango_with_django_project/', include('tango_with_django_project.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),

    url(r'^rango/', include('rango.urls')), # ADD THIS NEW TUPLE!
)

views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Rango says hello world!")

From the settings.py file

ROOT_URLCONF = 'tango_with_django_project.urls'

Hope you all can help get me started

解决方案

Let's say I have a Django project called FailBook, with two apps, posts and links. If I look into FailBook/urls.py, I will find something like

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

from django.contrib import admin
admin.autodiscover()

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

    url(r'^posts/', include('posts.urls')), ## Custom url include
    url(r'^links/', include('links.urls')), ## Custom url include
)

So then, when you look into the directory structure, you will notice that there are extra two urls.py files

FailBook
|-- posts
   |-- models.py
   |-- urls.py
   |-- views.py
   |-- etc.
|-- links
   |-- models.py
   |-- urls.py
   |-- views.py
   |-- etc.

# urls.py file in the posts folder
from django.conf.urls import patterns, include, url
from .views import PostListView, PostDetailView

urlpatterns = patterns('',

    url(r'^posts/', PostListView.as_view()), 
    url(r'^posts/(?P<post_id>\d+)', PostDetailView.as_view()),
)
# where both views are class based views, hence the as_view function call

这篇关于Django“否模块命名URL”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆