问题描述
我具有以下结构(Django 1.4):
contains_dir /
myproject /
myapp1 /
myapp2 /
myapp3 /
myproject,myapp1,myapp2和myapp3都具有 init .py,所以它们都是模块。
在manage.py中(在containing_dir下)我有 os.environ.setdefault( DJANGO_SETTINGS_MODULE, myproject.settings)
在我的myproject.settings中定义:
[..]
ROOT_URLCONF ='myproject.urls'
INSTALLED_APPS =(
[..]
'myproject.myapp1',
'myproject.myapp2',
'myproject.myapp3',
)
[..]
在myapp1.urls.py中,我定义:
urlpatterns = pattern('myapp1',
url(r'^ agent / $','views.agent',name ='agent')
)
,然后尝试将其导入myproject.urls,然后尝试导入myapp1 ur像这样:
(r'^ myapp1 /',include('myproject.myapp1.urls'))
但是每当我尝试加载 localhost:8000 / myapp1 / agent
我得到
异常值:没有名为myapp1
$ p的模块$ p>
我认为从myapp1.urls扔掉
有什么帮助吗?谢谢
解决方案您必须拥有
__ init__.py
文件位于 myproject目录中。当您说:
(r'^ myapp1 /',include('myproject.myapp1.urls'))
您是说 myproject(以及myapp1)是一个python packege。
I have the following structure (Django 1.4):
containing_dir/ myproject/ myapp1/ myapp2/ myapp3/
myproject, myapp1, myapp2, and myapp3 all have init.py, so they're all modules.
In manage.py (under containing_dir) I have
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
in myproject.settings i define:
[..] ROOT_URLCONF = 'myproject.urls' INSTALLED_APPS = ( [..] 'myproject.myapp1', 'myproject.myapp2', 'myproject.myapp3', ) [..]
In myapp1.urls.py I define:
urlpatterns = patterns('myapp1', url(r'^agent/$', 'views.agent', name='agent') )
and I try to import it in myproject.urls I try to import myapp1 urls like this:
(r'^myapp1/', include('myproject.myapp1.urls'))
but whenever I try lo load
localhost:8000/myapp1/agent
I getException Value: No module named myapp1
I think thrown from withing myapp1.urls
Any help? thanks
解决方案You must have a
__init__.py
file inside your "myproject" directory. When you say:
(r'^myapp1/', include('myproject.myapp1.urls'))
you are saying "myproject" (as well as myapp1) is a python packege.
这篇关于Django-包含应用网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!