在urls.py中导入视图时出错 [英] Error import views in urls.py
本文介绍了在urls.py中导入视图时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白为什么此行失败:从库导入视图中
I do not understand why this line fails: from library import views
from django.conf.urls import include, url
from library import views
urlpatterns = [
url(r'^$', IndexView.as_view()),
]
但这不是:从library.views导入IndexView
from django.conf.urls import include, url
from library.views import IndexView
urlpatterns = [
url(r'^$', IndexView.as_view()),
]
文件views.py
file views.py
from django.shortcuts import render
from django.views.generic import TemplateView
class IndexView(TemplateView):
template_name = "index.html"
推荐答案
您需要导入主类本身而不是父类.
You need to import the main class itself not the parent.
from django.conf.urls import include, url
from library import views
urlpatterns = [
url(r'^$', IndexView.as_view()), ## this will not work
url(r'^$', views.IndexView.as_view()), ## OK
]
在其他情况下
In the other scenario
from django.conf.urls import include, url
from library.views import IndexView
urlpatterns = [
url(r'^$', IndexView.as_view()), ## OK
]
这篇关于在urls.py中导入视图时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文