相同的功能在不同的视图 - Django [英] Same functions in different views - Django
问题描述
以下是我的目录结构:
C:。
│manage.py
│
├───books
│admin.py
│admin.pyc
│models.py
│models.pyc
│tests.py
│views.py
│views.pyc
│__init__.py
│__init __。pyc
│
└───mysite
base.html
base_blog.html
base_welcome.html
currdate.html
settings.py
settings.pyc
urls.py
urls.pyc
view.py
views.pyc
wsgi.py
wsgi.pyc
__init__.py
__init __。pyc
一个额外的相关问题 - 如果我有多个项目(如mysite ')和这些项目中的不同应用程序,如何区分项目中的URL?我的意思是,目前,URL http://127.0.0.1:8000/hello
映射到mysite项目中的视图函数。如果我现在还有一个项目,说mynewsite,Django如何将URL http://127.0.0.1:8000/hello
解析为 mynewsite?我希望我的问题很清楚。
谢谢,
Rakesh。
在python中,您可以导入库(在这种情况下为views.py的文件),其中一个将导致您正在讨论的名称冲突,即
从books.views导入my_view
从mysite.views导入my_view
$ b $在这种情况下,my_view将被覆盖。但是,您也可以执行以下
import books.views
import mysite.views
在这种情况下,要访问导入的视图,您将不得不使用完整路径,我。即books.views.my_view,因此没有名称冲突。
至于您的其他问题,请考虑以下urls.py内容:
urlpatterns = patterns('',
url(r'^ $',mysite.views.my_view,name ='index'),
您可以将其替换为
urlpatterns = patterns('',
/ pre>
url(r'^ $',books.views.my_view,name ='index'),
I have started learning Django recently and am following the code examples given in the tutorial of the Django site. Accordingly, I have created a site 'mysite' and an app 'books' in that site. Consequently, there are two 'views.py' files in the resultant directory structure - one in the 'mysite' folder and one in 'books' folder and there's only one 'urls.py' file - in the 'mysite' folder only. My question is the following - how do I avoid name conflicts for the functions that I create in the two 'views.py' files? If there are two functions with the same name in both the files, how does Django resolve which function to call for a given URL pattern in 'urls.py' file?
The following is my directory structure:
C:. │ manage.py │ ├───books │ admin.py │ admin.pyc │ models.py │ models.pyc │ tests.py │ views.py │ views.pyc │ __init__.py │ __init__.pyc │ └───mysite base.html base_blog.html base_welcome.html currdate.html settings.py settings.pyc urls.py urls.pyc views.py views.pyc wsgi.py wsgi.pyc __init__.py __init__.pyc
An additional and related question - if I have multiple projects (like 'mysite') and different apps in those projects, how do I differentiate the URLs across the projects? I mean, currently, the URL
http://127.0.0.1:8000/hello
is mapped to a view function in 'mysite' project. What if I now have one more project, say 'mynewsite', how would Django resolve the URLhttp://127.0.0.1:8000/hello
to the view function of 'mynewsite'? I hope my question is clear.Thanks, Rakesh.
解决方案In python you can import libraries (in this case file like views.py) one of them would be causing name conflicts you are talking about, namely
from books.views import my_view from mysite.views import my_view
in that case, my_view will simply be overwritten. However, you can also do the following
import books.views import mysite.views
In that case, to access the imported view, you will have to use full path, i. e. books.views.my_view, therefore there is no name conflict.
As for your other question, consider the following urls.py contents:
urlpatterns = patterns('', url(r'^$', mysite.views.my_view, name='index'),
You can replace it with
urlpatterns = patterns('', url(r'^$', books.views.my_view, name='index'),
这篇关于相同的功能在不同的视图 - Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!