Hi * people,
嗨*人,
In my clean function in forms.py, I would like to save automatically some information in a session variable. However, I do not seem to get access to the request variable.
在我干净的功能中。py,我想在一个会话变量中自动保存一些信息。但是,我似乎无法访问request变量。
All examples for handing over the request variable are based on function based views, but here I am using a class based view.
所有提交请求变量的例子都基于基于基于函数的视图,但是这里我使用的是基于类的视图。
My forms.py:
我的forms.py:
from django import forms
from item.models import Item
class CreateItemForm(forms.ModelForm):
class Meta:
model = Item
fields = ('name', 'description')
def __init__(self, request, *args, **kwargs):
self.request = request
super(CreateItemForm, self).__init__(*args, **kwargs)
def clean(self):
cleaned_data = super(CreateItemForm, self).clean()
if cleaned_data.get("address"):
self.request.session['name'] = cleaned_data.get("name")
else:
raise forms.ValidationError(_('Oops, can\'t find location.'))
return self.cleaned_data
My views.py:
我的views.py:
from django.views.generic.edit import FormView
from item.forms import CreateItemForm
class ItemCreate(FormView):
form_class = CreateItemForm
template_name = 'item/item_create.html'
success_url = 'http://www.google.com'
What is the best way to hand over the request
variable from the views.py to forms.py?
从视图中提交请求变量的最佳方式是什么?py forms.py吗?
Thank you for your answer.
谢谢你的回答。
2 个解决方案
#1
17
You can overwrite the FormMixin
's get_form_kwargs
method to add the request for to the form's init parameters:
您可以改写FormMixin的get_form_kwargs方法,将请求添加到表单的init参数中:
class ItemCreate(FormView):
def get_form_kwargs(self):
kwargs = super(ItemCreate, self).get_form_kwargs()
kwargs.update({
'request' : self.request
})
return kwargs
#2
0
Overriding the form.get_initial() works for me
重写form.get_initial()对我来说是可行的
class ItemCreate(FormView):
def get_initial(self):
init = super(ItemCreate, self).get_initial()
init.update({'request':self.request})
return init
And in the clean we can access it with form.initial dict
在清洁中我们可以用表格来访问它。最初的东西
class sampleForm(forms.Form):
...
...
def clean(self):
user_request = self.initial['request']
By the way, we don't need to pop the extra args like suggested above.
顺便说一下,我们不需要像上面建议的那样弹出额外的args。
#1
17
You can overwrite the FormMixin
's get_form_kwargs
method to add the request for to the form's init parameters:
您可以改写FormMixin的get_form_kwargs方法,将请求添加到表单的init参数中:
class ItemCreate(FormView):
def get_form_kwargs(self):
kwargs = super(ItemCreate, self).get_form_kwargs()
kwargs.update({
'request' : self.request
})
return kwargs
#2
0
Overriding the form.get_initial() works for me
重写form.get_initial()对我来说是可行的
class ItemCreate(FormView):
def get_initial(self):
init = super(ItemCreate, self).get_initial()
init.update({'request':self.request})
return init
And in the clean we can access it with form.initial dict
在清洁中我们可以用表格来访问它。最初的东西
class sampleForm(forms.Form):
...
...
def clean(self):
user_request = self.initial['request']
By the way, we don't need to pop the extra args like suggested above.
顺便说一下,我们不需要像上面建议的那样弹出额外的args。