4👍
✅
You should give the if form.is_valid() part, one more tab.
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from compare.forms import NameForm
def get_name(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
1👍
I’m not yet allowed to add comments to your question, but you did notice, that your view renders name.html while you’re providing the code for index.htm.
Basically your code seems valid, while it can be further optimized:
def get_name(request):
form = NameForm(request.POST or None)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
return render(request, 'name.html', {'form': form})
- [Django]-Don't include blank fields in GET request emitted by Django form
- [Django]-Django – Redirect user to "next" parameter after successful login
- [Django]-Attribute Error: 'QueryDict' object has no attribute 'iterlists'
- [Django]-Django formsets required
1👍
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
This should work by adding editing {{form.as_p}}
- [Django]-Google application engine, maximum number of static files?
- [Django]-How to customize pickle for django model objects
- [Django]-Django no sessionid in cookie
- [Django]-Django Meta ordering in related query
Source:stackexchange.com