Reputation: 4874
How do I get values from form fields in the django framework? I want to do this in views, not in templates...
Upvotes: 157
Views: 349748
Reputation: 11
The above solutions didn't work for me so I did it like this
class BlogActions(View):
def get(self, request, blog_id):
print(blog_id)
try:
blog = Blog.objects.get(id = blog_id)
except Blog.DoesNotExist:
pass
print(blog)
form = BlogForm(instance=blog)
context = {
'form' : form,
'title' : form['title'].value(),
'content' : form['content'].value()
}
return render(request, 'blogs/edit-blogs.html', context)
and on the templates I did this
<input name='title' class="w-full h-10 text-black" value="{{title}}" type="text">
<textarea name='content' class="w-full text-black h-56 resize-none" >{{content}}</textarea>
Same thing, but somehow the other one was not working
Upvotes: 0
Reputation: 352
Incase of django viewsets or APIView you can simply get the request.data and if you're sure the data being passed to the view is always form data then
data = request.data.copy()
data['key'][0]
to get the first element of the list which is the value first value and most likely only value if they key only returns a single value of the key.for example
class EquipmentView(APIView):
def post(self, request: Request):
data = request.data.copy()
author = data.get('author')[0]
Upvotes: 0
Reputation: 21
Cleaned_data converts the submitted form into a dict where the keys represent the attribute name used in form and value is user submitted response. to access a particular value in the views we write:
formname.cleaned_data['fieldname']
Upvotes: 1
Reputation: 1409
It is easy if you are using django version 3.1 and above
def login_view(request):
if(request.POST):
yourForm= YourForm(request.POST)
itemValue = yourForm['your_filed_name'].value()
# Check if you get the value
return HttpResponse(itemValue )
else:
return render(request, "base.html")
Upvotes: 4
Reputation: 307
To retrieve data from form which send post request you can do it like this
def login_view(request):
if(request.POST):
login_data = request.POST.dict()
username = login_data.get("username")
password = login_data.get("password")
user_type = login_data.get("user_type")
print(user_type, username, password)
return HttpResponse("This is a post request")
else:
return render(request, "base.html")
Upvotes: 18
Reputation: 17085
Take your pick:
def my_view(request):
if request.method == 'POST':
print request.POST.get('my_field')
form = MyForm(request.POST)
print form['my_field'].value()
print form.data['my_field']
if form.is_valid():
print form.cleaned_data['my_field']
print form.instance.my_field
form.save()
print form.instance.id # now this one can access id/pk
Note: the field is accessed as soon as it's available.
Upvotes: 113
Reputation: 1916
I use django 1.7+ and python 2.7+, the solution above dose not work. And the input value in the form can be got use POST as below (use the same form above):
if form.is_valid():
data = request.POST.get('my_form_field_name')
print data
Hope this helps.
Upvotes: 11
Reputation: 7587
You can do this after you validate your data.
if myform.is_valid():
data = myform.cleaned_data
field = data['field']
Also, read the django docs. They are perfect.
Upvotes: 33
Reputation: 188004
Using a form in a view pretty much explains it.
The standard pattern for processing a form in a view looks like this:
def contact(request):
if request.method == 'POST': # If the form has been submitted...
form = ContactForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
print form.cleaned_data['my_form_field_name']
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = ContactForm() # An unbound form
return render_to_response('contact.html', {
'form': form,
})
Upvotes: 167