作业有什么问题

时间:2011-01-20 19:12:07

标签: python django web

怎么了?

File "trunk/website/auth/user/emailchange/views.py" in email_change_view
102. context['form'] = form
File "/usr/local/lib/python2.6/dist-packages/django/template/context.py" in __setitem__
39. self.dicts[-1][key] = value

Exception Type: TypeError at /accounts/email/change/ Exception Value: 'SendUsernameForm' object does not support item assignment

以下是代码

else:
    form = EmailChangeForm(username=request.user.username)

context = RequestContext(request, extra_context)
context['form'] = form

return render_to_response(template_name, context_instance=context)

HTML代码:

<form method="post">{% csrf_token %}
            {{ form.as_p }}
            <p><input type="submit" value="Change email address" /></p>
</form>

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

form添加到extra_context段,而不是context

错误实际上在说,context['form']无效,所以这是一个开始想知道是否应该以这种方式使用RequestContext的好地方。

RequestContext docs:http://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.RequestContext

else:
    form = EmailChangeForm(username=request.user.username)

extra_context['form'] = form
context = RequestContext(request, extra_context)