Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:如何为FormView提供上下文?_Python_Django_Django Templates - Fatal编程技术网

Python Django:如何为FormView提供上下文?

Python Django:如何为FormView提供上下文?,python,django,django-templates,Python,Django,Django Templates,新手问题:我有一个FormView,它显示一个注册表单,使用Django的验证等。一切都很好,但是,我似乎不知道如何向模板提供任何数据(上下文)。目前,我有: from django.shortcuts import render from django.shortcuts import redirect from django.http import HttpResponse from accounts.forms import SignUpForm class SignUpView(For

新手问题:我有一个FormView,它显示一个注册表单,使用Django的验证等。一切都很好,但是,我似乎不知道如何向模板提供任何数据(上下文)。目前,我有:

from django.shortcuts import render
from django.shortcuts import redirect
from django.http import HttpResponse
from accounts.forms import SignUpForm

class SignUpView(FormView):
    template_name = 'accounts/signup.html'
    form_class = SignUpForm 

    def form_valid(self, form):
        # executes when form validates..
        (...)
        return redirect('/account/')
我尝试通过下面的
get()
添加一些上下文数据,这是我在页面第一次显示时需要的,除了表单的输入字段之外,其他哪种工作方式都没有了(标签都在那里):


有人能解释一下为什么会这样,以及如何让它发挥作用吗?换句话说:当使用FormView和form_valid()时,我应该将用于初始GET请求的代码放在哪里

您必须找到执行此操作的方法,如果数据仅与get视图相关,则您可以继续:

def get_context_data(self, **kwargs):
    context = super(SignUpView, self).get_context_data(**kwargs)
    something = something
    context['something'] = something
    return context
或者使用Mixin:

class SomeMixin(object):

    def get_context_data(self, **kwargs):
        context = super(SomeMixin, self).get_context_data(**kwargs)
        something = something
        context['something'] = something
        return context
然后:

class SignUpView(SomeMixin, FormView):

    def form_valid(self, form):
        ...

请看我在这个问题上的回答:谢谢!虽然我相信答案中有一个错误,但我还是解决了:在
ctx=returnsuper(…)
中不应该有
return
?哦,是的,谢谢,我没有给account我尝试了上面的第一种方法,但我看起来上下文并没有命中模板——在/account/signup/'NoneType'对象上获取
TypeError是不合适的
(应该是这样)。上面Victor的解决方案,使用
get\u context\u data(self,**kwargs)
实现了这个技巧。谢谢大家!很抱歉,我在第一个代码中犯了一个错误,它应该是get_context_data for the context,我现在正在编辑它。
class SignUpView(SomeMixin, FormView):

    def form_valid(self, form):
        ...