Django:从另一个应用程序导入视图 [英] Django: Importing a view from another app

查看:44
本文介绍了Django:从另一个应用程序导入视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目和一个应用程序.我正在尝试使用项目中的urls.py来激活应用程序中保存的视图.

I have a project and an app. I am trying to use the urls.py in the project to activate a view held in the app.

错误消息:导入homepage.indexModuleNotFoundError:没有名为"homepage.index"的模块

The error message: import homepage.index ModuleNotFoundError: No module named 'homepage.index'

在项目urls.py中,我有以下导入语句:

In the project urls.py I have this import statement:

from homepage.views import index

然后在项目的urls.py中,urlpatterns []数组将包含以下内容:引用该应用程序的视图:

Then in the project's urls.py, the urlpatterns[] array includes this reference to the app's view:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', homepage.views.index, name='index'),
]

homepage/views.py

def index(request):
    return HttpResponse("My Homepage")

哪里可能出错?

推荐答案

您没有正确导入.更改

url(r'^$', homepage.views.index, name='index'),

url(r'^$', index, name='index'),

这篇关于Django:从另一个应用程序导入视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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