Django URL 和视图
Django URL and Views
因为下面这个从视图映射 Django Url 的方法在 Django 1.9 及更高版本中被弃用了
url(r'^contact/$', 'newsletter.views.contact', name='contact'),
url(r'^about/$', 'home.views.about', name='about'),
这就是目前的情况
from newsletter import views'
url(r'^about/$', 'views.about', name='about'),
我如何为 2 个不同的应用视图映射我的 url
from home import views
from newsletter import views
url(r'^home/$', 'views.home', name='home'), #located in home
url(r'^about/$', 'views.about', name='about'), #located in newsletter
像我上面那样映射会导致错误,所以我需要帮助。 Django 新手
尝试:
from home import views as home_views
from newsletter import views
url(r'^home/$', 'home_views.home', name='home'), #located in home
url(r'^about/$', 'views.about', name='about'), #located in newsletter
我不明白你所说的使用完全限定的模块名称被弃用是什么意思,因为它是核心 python 结构。
但是您可以通过使用 "import as" 语句将它们绑定到不同的别名来管理包含具有相同名称的子模块的两个不同模块。
示例:
from home import views as home_view
from newsletter import views as news_view
然后您可以在整个声明的命名空间中使用别名 home_view
和 news_view
来引用每个模块而不是 views
。
您可以查看 Python 文档中的导入语句语法 here:
If the requested module is retrieved successfully, it will be made
available in the local namespace in one of three ways:
- If the module name is followed by as, then the name following as is
bound directly to the imported module.
- If no other name is specified,
and the module being imported is a top level module, the module’s name
is bound in the local namespace as a reference to the imported module
- If the module being imported is not a top level module, then the name
of the top level package that contains the module is bound in the
local namespace as a reference to the top level package. The imported
module must be accessed using its full qualified name rather than
directly
作为替代方案,您只能导入视图函数:
from home.views import home
from newsletter.views import about
urlpatterns = [
url(r'^home/$', home, name='home'),
url(r'^about/$', about, name='about'),
]
因为下面这个从视图映射 Django Url 的方法在 Django 1.9 及更高版本中被弃用了
url(r'^contact/$', 'newsletter.views.contact', name='contact'),
url(r'^about/$', 'home.views.about', name='about'),
这就是目前的情况
from newsletter import views'
url(r'^about/$', 'views.about', name='about'),
我如何为 2 个不同的应用视图映射我的 url
from home import views
from newsletter import views
url(r'^home/$', 'views.home', name='home'), #located in home
url(r'^about/$', 'views.about', name='about'), #located in newsletter
像我上面那样映射会导致错误,所以我需要帮助。 Django 新手
尝试:
from home import views as home_views
from newsletter import views
url(r'^home/$', 'home_views.home', name='home'), #located in home
url(r'^about/$', 'views.about', name='about'), #located in newsletter
我不明白你所说的使用完全限定的模块名称被弃用是什么意思,因为它是核心 python 结构。 但是您可以通过使用 "import as" 语句将它们绑定到不同的别名来管理包含具有相同名称的子模块的两个不同模块。
示例:
from home import views as home_view
from newsletter import views as news_view
然后您可以在整个声明的命名空间中使用别名 home_view
和 news_view
来引用每个模块而不是 views
。
您可以查看 Python 文档中的导入语句语法 here:
If the requested module is retrieved successfully, it will be made available in the local namespace in one of three ways:
- If the module name is followed by as, then the name following as is bound directly to the imported module.
- If no other name is specified, and the module being imported is a top level module, the module’s name is bound in the local namespace as a reference to the imported module
- If the module being imported is not a top level module, then the name of the top level package that contains the module is bound in the local namespace as a reference to the top level package. The imported module must be accessed using its full qualified name rather than directly
作为替代方案,您只能导入视图函数:
from home.views import home
from newsletter.views import about
urlpatterns = [
url(r'^home/$', home, name='home'),
url(r'^about/$', about, name='about'),
]