相同的功能在不同的视图 - Django [英] Same functions in different views - Django

查看:78
本文介绍了相同的功能在不同的视图 - Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Django,并遵循Django网站教程中给出的代码示例。因此,我在该网站上创建了一个网站mysite和一个应用程序的书。因此,最终目录结构中有两个views.py文件,一个在mysite文件夹中,一个在books文件夹中,只有一个urls.py文件仅在mysite文件夹中。我的问题如下 - 如何避免在两个'views.py'文件中创建的函数的名称冲突?如果两个文件中有两个相同名称的函数,Django如何解析在urls.py文件中调用给定URL模式的函数?



以下是我的目录结构:

  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('',
url(r'^ $',books.views.my_view,name ='index'),
/ pre>

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 URL http://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屋!

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