Django forms not receiving input
I'm using Django's forms.py method to build a form, but am not getting any data when trying to get the input by doing request.POST.get('value') on it. For every print statement that gave a response, I put a comment next to the command with whatever it returned in the terminal.
Additionally, I do not understand what the action = "address" is meant for in the form. Finally, the csrf verification was not working and kept returning CSRF verification failed so I disabled it, but if anyone knows how to get this working I would appreciate it.
forms.py
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Enter name:', max_length=100)
views.py
@csrf_exempt
def blank(request):
if request.method == 'POST':
get = request.POST.get
print(f"get: {get}") # get: <bound method MultiValueDict.get of <QueryDict: {}>>
print(f"your_name: {request.POST.get('your_name')}") # your_name: None
form = NameForm(request.POST)
if form.is_valid():
your_name = form.cleaned_data["your_name"]
print(f"your_name cleaned: {your_name}")
return HttpResponseRedirect('/thanks/')
else:
form = NameForm()
return render(request, 'blank.html', {'form': form})
blank.html
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form action="" method="post">
{{ form }}
<input type="submit" value="Submit">
</form>
</body>
</html>
Try to do this.. and must include csrf otherwise it will not consider post request..
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
</body>
</html>