Python 没有名为HelloTemplate的模块导入错误
我收到一个导入错误。它说没有名为HelloTemplate的模块。但是有一个HelloTemplate类,我已经在urls.py文件中导入了它。 登录名是我的django应用程序名 这是my views.py文件Python 没有名为HelloTemplate的模块导入错误,python,django,Python,Django,我收到一个导入错误。它说没有名为HelloTemplate的模块。但是有一个HelloTemplate类,我已经在urls.py文件中导入了它。 登录名是我的django应用程序名 这是my views.py文件 #from django.shortcuts import render from django.http import HttpResponse from django.template.loader import get_template from django.template
#from django.shortcuts import render
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
from django.shortcuts import render_to_response
from django.views.generic.base import TemplateView
# Create your views here.
def hello(request):
name='Zeeshan'
html="<html><body> hi this is %s.</body></html>" %name
return HttpResponse(html)
def hello_template(request):
name='zeeshan'
t=get_template('hello.html')
html=t.render(Context({'name':name}))
return HttpResponse(html)
class HelloTemplate (TemplateView):
template_name="hello_class.html"
def get_context_data(self, **kwargs):
context=super(HelloTemplate, self).get_context_data(**kwargs)
context["name"] = "zee"
return context
这是在本地服务器上运行时的错误报告
ImportError at /hello_class_view/
No module named HelloTemplate
Request Method: GET
Request URL: http://127.0.0.1:8000/hello_class_view/
Django Version: 1.8.4
Exception Type: ImportError
Exception Value:
No module named HelloTemplate
Exception Location: /usr/lib/python2.7/importlib/__init__.py in import_module, line 37
Python Executable: /home/grayhat/Documents/python/projects/bin/python
Python Version: 2.7.6
Python Path:
['/home/grayhat/Documents/python/projects/myapp',
'/home/grayhat/Documents/python/projects/local/lib/python2.7/site-packages/Django-1.8.4-py2.7.egg',
'/home/grayhat/Documents/python/projects/lib/python2.7/site-packages/Django-1.8.4-py2.7.egg',
'/home/grayhat/Documents/python/projects/lib/python2.7',
'/home/grayhat/Documents/python/projects/lib/python2.7/plat-x86_64-linux-gnu',
'/home/grayhat/Documents/python/projects/lib/python2.7/lib-tk',
'/home/grayhat/Documents/python/projects/lib/python2.7/lib-old',
'/home/grayhat/Documents/python/projects/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/grayhat/Documents/python/projects/local/lib/python2.7/site-packages',
'/home/grayhat/Documents/python/projects/lib/python2.7/site-packages']
Server time: Mon, 21 Sep 2015 08:06:38 +0000
url.py“HelloTemplate.as_视图”中的代码段应为HelloTemplate.as_视图。这是,没有引号。您可以查看有关引用基于类的视图的详细信息。您的views.py和urls.py在同一目录中?不,两者都在不同的目录中。views.py在登录目录中,urls.py在myapp目录中。顺便说一句,您需要从login导入这两个名称。视图:从login.views导入hello\u模板,hellotemplate还可以在views.py中编写hellotemplate=hellotemplate.as_视图,在URL中编写urlr“^hello\u class\u view/$”、“login.views.hellotemplate”。py@user2172884可以,谢谢你的反馈。然而,我认为为了惯例和混淆OP的风险,最好坚持django网站上记录的方式。
ImportError at /hello_class_view/
No module named HelloTemplate
Request Method: GET
Request URL: http://127.0.0.1:8000/hello_class_view/
Django Version: 1.8.4
Exception Type: ImportError
Exception Value:
No module named HelloTemplate
Exception Location: /usr/lib/python2.7/importlib/__init__.py in import_module, line 37
Python Executable: /home/grayhat/Documents/python/projects/bin/python
Python Version: 2.7.6
Python Path:
['/home/grayhat/Documents/python/projects/myapp',
'/home/grayhat/Documents/python/projects/local/lib/python2.7/site-packages/Django-1.8.4-py2.7.egg',
'/home/grayhat/Documents/python/projects/lib/python2.7/site-packages/Django-1.8.4-py2.7.egg',
'/home/grayhat/Documents/python/projects/lib/python2.7',
'/home/grayhat/Documents/python/projects/lib/python2.7/plat-x86_64-linux-gnu',
'/home/grayhat/Documents/python/projects/lib/python2.7/lib-tk',
'/home/grayhat/Documents/python/projects/lib/python2.7/lib-old',
'/home/grayhat/Documents/python/projects/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/grayhat/Documents/python/projects/local/lib/python2.7/site-packages',
'/home/grayhat/Documents/python/projects/lib/python2.7/site-packages']
Server time: Mon, 21 Sep 2015 08:06:38 +0000