Reputation: 113
I'm doing an app and for some strange reason, I am not able to store the values of the form to the database. The problem is that the data are not sent via POST, therefore my view doesn't collect them. What would be the reason for that? My (simplified) Template code:
{% block content %}
<form action = "{%url 'bodySettings' %}" method = "POST">
{% csrf_token %}
{{ form }}
<input type = "submit" value = "Save"/>
</form>
{% endblock content %}
And my django view:
@login_required
@csrf_protect
def bodysettings(request):
properties_Form = BodyPropertiesForm()
if request.method == 'POST':
print ("test")
if properties_Form.is_valid():
properties = properties_Form.save(commit = False)
properties.user = request.user
properties.save()
return HttpResponseRedirect('/home/')
args = {}
args.update(csrf(request))
args['request'] = request
args['form'] = properties_Form
return render_to_response('stats_test.html',args)
And my forms.py:
from django import forms
from django.contrib.auth.models import User
from models import Body
class BodyPropertiesForm(forms.ModelForm):
MALE = 'M'
FEMALE = 'F'
GENDER_CHOICE = (
(MALE, 'male'),
(FEMALE, 'female'),
)
gender = forms.ChoiceField(choices = GENDER_CHOICE, required = True,
help_text = "Select your gender")
height = forms.IntegerField(required = True, help_text = "Enter your height")
weight = forms.IntegerField(required = True, help_text = "Enter your body weight" )
neck = forms.IntegerField(required = True, help_text = "Enter your neck size")
shoulders = forms.IntegerField(required = True, help_text = "Enter your width")
chest = forms.IntegerField(required = True, help_text = "Enter your chest size")
arm = forms.IntegerField(required = True, help_text = "Enter your arm size")
wrist = forms.IntegerField(required = True, help_text = "Enter your wrist size")
hips = forms.IntegerField(required = True, help_text = "Enter your waist size ")
waist = forms.IntegerField(required = True, help_text = "Enter your hips size")
thigh = forms.IntegerField(required = True, help_text = "Enter your thigh size")
calf = forms.IntegerField(required = True, help_text = "Enter your calf size")
class Meta:
model = Body
fields = ('gender','height','weight','neck','shoulders','chest','arm','wrist',
'waist','hips','thigh','calf')
exclude = ('user',)
Upvotes: 0
Views: 1121
Reputation: 20339
You can see in terminal whether it is post
or get
request.Here data will be post
i think.The problem which query dict
is empty is because,
class NameForm(forms.Form):
your_name = forms.CharField(label='myfieldname', max_length=100)
So label
should be there in form.This can be fetch in view
as request.POST['myfieldname']
.Also you have to use
properties_Form = BodyPropertiesForm(request.POST)
Hope this helps
Upvotes: 0
Reputation: 599580
You're not passing your POST values to the form.
if request.method == 'POST':
properties_Form = BodyPropertiesForm(request.POST)
Upvotes: 2