Answer the question
In order to leave comments, you need to log in
What is the most correct way to handle the simplest form data in Django?
Hello.
For educational purposes, I set myself the following task:
Create a page where the user enters his name in a form field, after which he goes to a page where he is greeted by name. That is, the user enters the name "Vasya" and sees the greeting "Hello, Vasya" on another page. In the draft,
the problem is solved in the following way:
There is a Put_your_name view that accepts the user's name.
def Put_your_name(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/Hello_user/')
else:
form = NameForm()
return render(request, 'Put_your_name.html', {'form': form})
class NameForm(forms.Form):
your_name = forms.CharField(label='Введите свое имя:', max_length=20)
def Hello_user(request):
name_of_user = request.POST['your_name']
return render(request, 'Hello_user.html', {'form_name': name_of_user})
<form action="/Hello_user/" method="POST">
{% csrf_token %}
{{form}}
<p><input type="submit" value="Привет" /></p>
</form>
<h1>Привет, {{form_name}}</h1>
urlpatterns = [
url ('^name/$', Put_your_name),
url ('^Hello_user/$', Hello_user),
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question