gpt4 book ai didi

python - 没有名为 HelloTemplate 的模块导入错误

转载 作者:行者123 更新时间:2023-11-28 22:41:21 24 4
gpt4 key购买 nike

我收到一个导入错误。它说没有名为 HelloTemplate 的模块。但是我在 urls.py 文件中导入了一个 HelloTemplate 类。“登录”是我的 Django 应用名称。

这是我的 views.py 文件。

#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

这是 url.py 文件..

from django.conf.urls import include, url
from django.contrib import admin
from login.views import HelloTemplate

urlpatterns = [
#url(r'^admin/', include(admin.site.urls)),
url(r'^hello/$', 'login.views.hello'),
url(r'^hello_template/$', 'login.views.hello_template'),
url(r'^hello_class_view/$','HelloTemplate.as_view()'),
]

这是在本地服务器上运行时的错误报告。

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

最佳答案

urls.py 'HelloTemplate.as_view()' 中的片段应该是 HelloTemplate.as_view()。这是,没有引号。您可以查看更多关于引用基于类的 View here .

关于python - 没有名为 HelloTemplate 的模块导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32690488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com