带get方法的FormView不显示表单

时间:2016-06-08 05:26:42

标签: django formview

感谢您阅读:/

当我删除" get"从视图来看,表单有效,但是当我把它放回去时,它不会渲染。

浏览器的网址: http://127.0.0.1:8000/activate?tpr=1140277&idpr=42

网址:

url(r'^activate', Activation_vw.as_view(), name='activate')

查看:

class Activation_vw(FormView):
    template_name = 'activate.html'
    form_class = Activation_Form
    success_url = '/dashboard/'

    def get(self, request, *args, **kwargs):
        tokenProspect_v = request.GET.get('tpr')
        idProspect_v = request.GET.get('idpr')

        USER = USERS.objects.filter(
            id=idProspect_v).values('id', 'email', 'token')

        if int(tokenProspect_v) != int(USER[0]['token']):
            message = "Check the URL"
        else:
            message = USER[0]['email']

        context = {'msg': message}
        return self.render_to_response(context)

    def form_valid(self, form):
        # No code yet
        return super(Activation, self).form_valid(form)

FORM:

class Activation_Form(forms.Form):
    email = forms.EmailField()
    password = forms.CharField(widget=forms.PasswordInput())

TEMPLATE:

Hello, {{ msg }}
<form action="" method="POST"> 
    {%csrf_token%}
    {{ form.as_p }}
    <button type="submit">Activate</button>
</form>

所有这些都在每个文件的顶部都有导入。

get功能完美无缺,我收到tpridprform没有收到,因此表格{{1}并且form_valid也不起作用。

我怀疑在success_url返回时出现了问题,但无法理解。

1 个答案:

答案 0 :(得分:0)

这是因为您的form未在上下文中传递:

context = {'msg': message}
return self.render_to_response(context)

您可以使用get_context_data获取上下文并随时更新:

context = self.get_context_data(msg=message)
return self.render_to_response(context)

这将从超类调用get_context_data,并将form包含在上下文中,同时添加message