Django form, request.post and initial - django

I have a django form where I need to set a value for validation purposes which is not passed in as part of the standard Post request.
In my code I currently have something like this:
if request.method == 'POST':
postform = CreatePostForm(request.POST, request.FILES, initial={'post_type':post.post_type})
if postform.is_valid():
.....
The value post_type is a selection with a value of something like 'QUE'
The issue I am having is that this does not appear to be valid. Does anyone have any suggestions on how to add the post_type value into the CreatePostForm class before the validation takes place.
Please note I do not want to expose this value on the form so including it as part of the post is not an option.

One approach is to make it a property on the form class that you hand in as an argument when you instantiate it:
class CreatePostForm(forms.Form/ModelForm):
def __init__(self, post_type, *args, **kwargs):
super(CreatePostForm, self).__init__(*args, **kwargs)
self.post_type = post_type
postform = CreatePostForm(post_type=post.post_type, request.POST, request.FILES)
Hope that helps you out.

Related

Django Forms - how to check in __INIT__ method whether request.POST was provided

After two years of experience with Django forms, I ran into the following dilemma related to __init__ method:
I have a Django form definition as follows:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(MyForm, self).__init__(*args, **kwargs)
if not MyModel.objects.filter(user = self.user).exists():
self.fields['field_1'].widget = forms.HiddenInput()
self.fields['field_2'].widget.attrs['placeholder'] = 'Enter value'
This code is ok if I initialize the form like this:
my_form = MyForm()
However, the problem arises when I try to save user input in the following way:
my_form = MyForm(request.POST)
My point is that I do not want to waste code execution time for setting placeholder property or deciding upon whether some field should be hidden or not AFTER the user has already submitted form.
My concern is that maybe that's because I misuse __init__ method?
Is there any way to check whether request.POST parameter has been provided? And if yes, is it considered best-practice to perform this check and do thinks like settings placeholder, initial values, etc. only if request.POST is not provided?
You can check self.is_bound; it's only true if data is passed to the form.
However, I really think you're over-optimising here. This will only have a tiny impact on the performance of the code.

Can't get dynamic python ModelChoiceField to work

I'm new to python and trying to understand how to get a dynamic ModelChoiceField to work. It works fine when I select an object with all but I'm trying to get the dropdown to reflect a user's attribute. Here is my code:
Forms.py
class ViewByMake(forms.Form):
dropdown = forms.ModelChoiceField(queryset=Make.objects.none())
def __init__(self, user, *args, **kwargs):
user = kwargs.pop('user')
super(ViewByMake, self).__init__(*args, **kwargs)
qs = Make.objects.filter(user=user)
self.fields['dropdown'].queryset = qs
self.fields['dropdown'].widget.attrs['class'] = 'choices1'
self.fields['dropdown'].empty_label = ''
Views.py
def view_bymake(request):
form = ViewByMake(request.POST or None, user=request.user)
if request.method == 'POST':
if form.is_valid():
make = form.cleaned_data['dropdown']
return HttpResponseRedirect(make.get_absolute_url1())
return render(request,'make/view_make.html',{'form':form})
This code works fine if I remove all user= references but then only returns the full make objects list which is not what I want. I found a very similar question on StackOverflow, but when I duplicated the code identically, it still doesn't work and it is giving me the following error:
init() got multiple values for argument 'user'
I searched the end of the internet on this topic. I'm open to other ideas if I'm approaching this poorly. I'm trying to basically get a filtered list based on criteria associated with a user's profile. I definitely need the drop down field to be specific to a user based on a profile setting. Thanks for your help in advance. I'm running django 1.11.2 and Python 3.6.1.
This is the updated model which need to include the user attribute which I didn't realize that I had to specify:
class Make(models.Model):
name = models.CharField(max_length=264,unique=True)
user = models.ForeignKey(User,null=True,on_delete=models.CASCADE)
Try with request, send request from form and get request in init method of form
views.py
def view_bymake(request):
form = ViewByMake(request.POST or None, request=request)
forms.py
def __init__(self, user, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(ViewByMake, self).__init__(*args, **kwargs)
qs = Make.objects.filter(user=self.request.user)
self.fields['dropdown'].queryset = qs
self.fields['dropdown'].widget.attrs['class'] = 'choices1'
self.fields['dropdown'].empty_label = ''
The answer to my original question, how do I get user=user to work consists of making sure that your form, view, and model all reference user. I originally had the user reference in the view and the form correct, but I neglected to make sure user= was specified on the model I was referencing. I thought it was built in, but turns out you have to specifically reference it on your model. I'm new at this so it was a learning experience. On to the next challenge!

pass multiple parameters to form from html table in django

I am newbie with Django and I get stucked trying to pass the value from a html table rendered with django-tables2 to a form.
view.py
def configView(request):
form = ConfigForm(request.POST or none)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
Messages.success(request, 'Configuracion Actualizada')
return HttpResponseRedirect('/monitor/')
return render_to_response("config.html",
locals(),
context_instance=RequestContext(request))
This is my forms.py
class ConfigForm(forms.ModelForm):
class Meta:
model = Config
def __init__(self, *args, **kwargs):
super(ConfigForm, self).__init__(*args,**kwargs)
self.fields['id_proveedor'].initial = kwargs.pop('id_proveedor',None)
But I don't know how to retrieve and pass the value to theform.
I need pass the values from the cells 0, 2 and 6.
Any advice or snippet will be appreciated.
Thanks in advance
I would try this:
class ConfigForm(forms.Form):
def __init__(self, *args, **kwargs):
your_variable_to_pass = kwargs.pop("your_variable_to_pass")
super(ConfigForm, self).__init__(*args,**kwargs)
self.fields['id_proveedor']= forms.FieldClass(attribute=your_variable_to_pass)
id_proveedor = FieldClass()
where, 'FieldClass' is whatever field you choose (i.e. ChoiceField, CharField) and
attribute is the attribute to pass (your variable), i.e. 'choices', 'initial' etc.
thus, it may look like this:
self.fields['id_proveedor']= forms.ChoiceField(choices=your_variable_to_pass)
id_proveedor = ChoiceField()
Notice indentation - you assign value of the attribute to pass in the constructor!; in case of ChoiceField choices is a list of tuples, i.e. (('1', 'First',), ('2', 'Second',)); I use Forms instead of ModelForm as super or base class in this example
Then, in the views: f = ConfigFrom(request.POST, your_variable_to_pass=your_variable_to_pass)
notice your_variable_to_pass=your_variable_to_pass otherwise it'll generate a key error
I hope, it helps!

Django form with __init__ doesn't clean fields

I have a django form I'm attempting to add CAPTCHA support to. However, this requires me to pass request.session to the form. To accomplish that, I added this constructor to the form:
def __init__(self, request=None, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
self.request = request
I then instantiate the RegistrationForm by passing the request object to it. However, when submitting the form, it fails to clean any of the fields. It just fails validation and passes a blank field back to the template. Here is the registration code that fails:
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
...do registration...
else:
return render(request, 'template.htm', {'form': form})
No matter what I put in the fields, it never validates. In fact, it doesn't even look like it cleans the fields. I just get back a new blank form after hitting the register button, no errors, nothing.
Any ideas?
Based on the code you posted, it looks as though you are passing request.POST into the request parameter for your RegistrationForm. i.e. you are doing the equivalent of:
form = RegistrationForm(request=request.POST)
What you really want to do is this:
form = RegistrationForm(request=request, data=request.POST)
Try this and see if it works for you.

Django Form field initial value on failed validation

how do I set the value of a field element after a form has been submitted but has failed validation? e.g.
if form.is_valid():
form.save()
else:
form.data['my_field'] = 'some different data'
I don't really want to put it in the view though and would rather have it as part of the form class.
Thanks
The documentation says:
If you have a bound Form instance and want to change the data somehow, or if you want to bind an unbound Form instance to some data, create another Form instance. There is no way to change data in a Form instance. Once a Form instance has been created, you should consider its data immutable, whether it has data or not.
I cannot really believe that your code works. But ok. Based on the documentation I would do it this way:
if request.method == 'POST':
data = request.POST.copy()
form = MyForm(data)
if form.is_valid():
form.save()
else:
data['myField'] = 'some different data'
form = MyForm(initial=data)
I ended up doing
if request.method == 'POST':
new_data = request.POST.copy()
form = MyForm(data=new_data)
if form.is_valid():
form.save()
else:
new_data['myField'] = 'some different data'
Hope this helps someone
You can put it in the form class like this:
class MyForm(forms.Form):
MY_VALUE = 'SOMETHING'
myfield = forms.CharField(
initial=MY_VALUE,
widget=forms.TextInput(attrs={'disabled': 'disabled'})
def __init__(self, *args, **kwargs):
# If the form has been submitted, populate the disabled field
if 'data' in kwargs:
data = kwargs['data'].copy()
self.prefix = kwargs.get('prefix')
data[self.add_prefix('myfield')] = MY_VALUE
kwargs['data'] = data
super(MyForm, self).__init__(*args, **kwargs)
The way it works, is it tests to see if any data has been passed in to the form constructor. If it has, it copies it (the uncopied data is immutable) and then puts the initial value in before continuing to instantiate the form.