1

I have a view that is processing a POST form in Django. I wanted to do some validation on the information. And then after, redirect to a view, using the data from the form. I wouldn't like to keep this information in the URL. What is the best way to do this?

Thanks.

corey
  • 13
  • 1
  • 5

2 Answers2

1

UPDATE:

If you want to process some data from one view in another view, you can do something like below:

def process_view(request, username):
    ''' This is the view where you want to process the username '''
    # process the username ...
    return something

def login_view(request):
    ''' Main login form through which data is submitted '''
    if request.method == 'POST':
        username = request.POST['username'] # the username submitted via form
        # do something ...
        # call the process_view below
        return process_view(request, username)

If, however, you want to login the user, you would need to set an authentication cookie on that user's browser. Setting cookies will also allow you to process the username in any view you want.

Here's how you can set cookies:

def login_view(request):
    if request.method == 'POST':
        username = request.POST['username']

        # set the cookie below
        request.session['username'] = username

        return something

And now, if you want to access the username in any other view, do this:

def some_view(request):
    username = request.session['username']
    # do something ...
    return something

I suggest you go with the second approach i.e. the setting cookies approach, since it is more effective than calling a view from another view.

xyres
  • 20,487
  • 3
  • 56
  • 85
  • Will the view that /thanks/ refers to have the data from the POST request from def form_view? That is my intention. So form_view processes a login and has a user name. Then I want to use that username in a different view. – corey Mar 16 '14 at 18:42
  • @corey I think I had misunderstood your question. Hang on while I update it. – xyres Mar 17 '14 at 14:16
  • @corey Hi, I've updated my answer. See if it helps ya. – xyres Mar 17 '14 at 14:59
0

Can you not call the other view with the POST data, at the end of your view?

Or use a session (or cookies) to store the post data? https://docs.djangoproject.com/en/dev/topics/http/sessions/

Otherwise see Response.Redirect with POST instead of Get?

Community
  • 1
  • 1
synotna
  • 51
  • 2
  • I've tried to call the other view with the POST data but am having difficulties. Do you know the proper way to do this? – corey Mar 16 '14 at 22:11