在我的views.py
文件中,我目前有
from django.contrib import messages
from django.http import HttpResponse
from django.core.urlresolvers import reverse
from django.shortcuts import redirect, get_object_or_404, render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.base import View, TemplateView
from models import *
from forms import *
class test(TemplateView):
def test(request):
return HttpResponse("test")
def get_template_names(self):
return ["myapp/test.html"]
urls.py
from django.conf.urls import patterns, url
from views import test
urlpatterns = patterns('',
url(r'$', test.as_view(), name='test'),
)
test.html
{% extends "index.html" %}
{% block content %}
<P> HELLO </P>
{% endblock %}
除非网站加载时没有显示任何内容!
index.html文件加载正常,因为我正在加载的html文件正在扩展它。但是,当我点击连接到网址&#39;测试&#39;在urls.py没有任何更新。 text.html文件只有文本&#34; hello world&#34;。
答案 0 :(得分:2)
在urls.py
中,您需要正确导入视图:
from django.conf.urls import patterns, url
from yourapp.views import test
urlpatterns = patterns('',
url(r'$', test.as_view(), name='test'),
)
在您的观看中,您需要修复您的课程,因此:
class test(TemplateView):
template_name = 'myapp/test.html'
您不应使用get_template_names
如果这是您要做的所有事情(即渲染模板),那么您可以直接在urls.py中导入TemplateView:
from django.conf.urls import patterns, url
# from yourapp.views import test
from django.views.generic.base import TemplateView
urlpatterns = patterns('',
url(r'$', TemplateView.as_view(template_name='myapp/test.html'), name='test'),
)
答案 1 :(得分:0)
尝试写下这样的观点:
class Test(TemplateView):
template_name = "myapp/test.html"
这是设置模板名称的推荐方法。