我嘗試不同類型的視圖實踐,我在Django 1.5得到以下錯誤信息: -關鍵字參數
context = super(HelloTemplate, self).get_context_data(**kwargs)
NameError: global name 'kwargs' is not defined.
我urls.py項目:
urlpatterns = patterns('',
url(r'^hello/$', 'article.views.hello'),
url(r'^hello_template/$', 'article.views.hello_template'),
url(r'^hello_template_simple/$', 'article.views.hello_template_simple'),
url(r'^hello_class_view/$', HelloTemplate.as_view()),
)
我的看法。潘岳: -
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
def hello(request):
name = 'Mudassar'
html = "<html><body>Hi %s, this seems to worked!</body></html>" % name
return HttpResponse(html)
def hello_template(request):
name = 'Mudassar'
t = get_template('hello.html')
html = t.render(Context({'name': name}))
return HttpResponse(html)
def hello_template_simple(request):
name = 'Mudassar'
return render_to_response('hello.html', {'name':name})
class HelloTemplate(TemplateView):
template_name = 'hello_class.html'
def get_context_data(self, **kwarg):
context = super(HelloTemplate, self).get_context_data(**kwargs)
context['name'] = 'Mudassar'
return context
這樣做的伎倆...謝謝很多傢伙..感謝您的幫助。 – user3114321