gpt4 book ai didi

python - 关键字参数

转载 作者:太空宇宙 更新时间:2023-11-04 08:11:00 24 4
gpt4 key购买 nike

我正在尝试不同类型的 View 进行练习,但在 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()),
)

我的观点.py: -

    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

最佳答案

因为 get_context_data 中的参数名为 kwarg 并且您通过 kwargs(复数形式)引用它。

我建议你使用复数形式的 kwargs 因为 is more standard :)

关于python - 关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22400587/

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