N
N
Night Tarlis2015-12-12 20:12:45
Django
Night Tarlis, 2015-12-12 20:12:45

How to access data entered by the user into a form?

I do not understand how to get the data that I collected from the forms?
In the documentation, an example, as I understand it, in HttpResponseRedirect, instead of /thanks/ there should be a link where I redirect the data received via request.POST from the form. But I'm not passing the variable itself, I'm just doing a redirect? How can I display this data from forms in the new template (/thanks/) - call another function in urls, but how can I pass data from forms to it, its scope is limited by its own limits?
view.py

from .forms import NameForm

def get_name(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = NameForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/thanks/')
    else:
        form = NameForm()
    return render(request, 'name.html', {'form': form})

I apologize if the questions are silly, the first language for the VP that I began to study. Thanks in advance :)
Forms are built on top of form.Form

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly F., 2015-12-12
@tarlis

That is, on the server, after receiving the POST, do you want to redirect to another page / controller and there so that there is data?
This is possible only with saving data to the database (the application context will not be useful here).

O
Oscar Django, 2015-12-13
@winordie

By the way, an interesting question. It's a crazy idea to throw in the cache, on /thanks/ pull from the cache and delete. Instead of a cache, you can use redis as a buffer.

Z
zigen, 2015-12-12
@zigen

if form.is_valid():
            #Сохраняем данные полученные из формы
            form.save()
            # redirect to a new URL:
            return HttpResponseRedirect('/thanks/')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question