这是我的代码:
def contact(request):
if request.method == 'POST':
form = forms.ContactForm(request.POST)
if form.is_valid():
message = 'Thanks for your post.'
user_name = form.cleaned_data['user_name']
user_city = form.cleaned_data['user_city']
user_school = form.cleaned_data['user_school']
user_email = form.cleaned_data['user_email']
user_message = form.cleaned_data['user_message']
ctx = {
'user_name':user_name,
'user_city':user_city,
'user_school':user_school,
'user_email':user_email,
'user_message':user_message,
}
else:
message = 'Please check the format of your information.'
else:
form = forms.ContactForm()
template = get_template('contact.html')
request_context = RequestContext(request)
request_context.push(locals())
html = template.render(request_context)
return HttpResponse(html)
但我得到以下例外:
TypeError: context must be a dict rather than RequestContext.
如何修改代码以避免此错误? (我的英语很差,对不起)
答案 0 :(得分:0)
不推荐将Context
对象传递给Template.render()
。
尝试更改此内容:
request_context = RequestContext(request)
request_context.push(locals())
html = template.render(request_context)
对此:
html = template.render(context=locals(), request=request)