1

I just started Django and I have a few fundamental questions about handling requests.

Say if I have two functions.

If I have a view, say

def test():
    return render(request, 'form.html')

And in form.html I have a form with method="POST" and action="/submitted/"

So now if I want to do something with the submitted data of the form, am I supposed to do it in the same test() view or do I have to do it in another view which would handle the `/submitted/ URL?

I've been doing it with the latter but then I read this: Need a minimal Django file upload example. Here, in the 4th point about views.py, they've made the form in the same view as they've handled the form.

How does that work? It seems very unintuitive. Or am I missing something?

Community
  • 1
  • 1
user1265125
  • 2,608
  • 8
  • 42
  • 65

3 Answers3

2

In django, the GET and POST methods can be handled in the same view, unless you want to post your form to a URL which is different from the current URL. Read the following code:

urls.py

url(r'^test/$', 'myapp.views.test', name='test'),
url(r'^test_post/$', 'myapp.views.test_post', name='test_post'),

views.py

def test(request):
    #Do your regular get method processes here
    if request.POST:
        #Do something with post data here
    return render_to_response('form.html', locals(), context_instance = RequestContext(request))

def test_post(request):
    if request.POST:
        #Do something with post data here
    return render_to_response('form.html', locals(), context_instance = RequestContext(request))

If you want your post data to be handled in the same view test include the set action="." in your form as given below.

<form method="post" action="." id="form_id" name="form_name">

If you want your post data to be handled in different view test_post include the set action="/test_post/" in your form as given below.

<form method="post" action="/test_post/" id="form_id" name="form_name">
arulmr
  • 8,620
  • 9
  • 54
  • 69
  • So if my form has `
    `, I will handle my post data in the view handling /application/, right? So if I want to handle my request in the same view, what would I put in `
    – user1265125 Apr 17 '13 at 07:37
  • 1
    If you want to handle post data in same view use `
    ` in your template.
    – arulmr Apr 17 '13 at 07:47
1

In your view you'll check if the form is posted or not, like this.

def test(request): # A view always needs a request
    if request.method == "POST":
        # Do validation stuff here
    else:
        return render(request, 'form.html')

But i suggest you take a look at Django forms, it's a built-in function that makes forms and validation so much easier.

J. Ghyllebert
  • 2,009
  • 1
  • 29
  • 35
  • I'm confused. How does the request.POST data from my page get to the `test` view? I thought it would be directed to the view handling `/submitted/` URL. Lets avoid Django forms for now. – user1265125 Apr 17 '13 at 07:29
  • It depends on whether view was coupled to that URL in your urls.py script anyway. But whatever view you would be writing, if it should accept POST data you'll check it this way. – J. Ghyllebert Apr 17 '13 at 07:35
0

The answers above are great, but if you're still a bit confused, let's break down some of the information you provided to see if we can understand the core problem, which is identifying what happens when.

If in the case your function "test()" is mapped to a URL such as "/test/"... then we can also assume that you're trying to figure out what happens when you submit to the URL "/submitted/".

  1. Where ever the "form action" is pointed to during form submission is where the data goes (e.g. /submitted/).

  2. If you told the form to submit to "/submitted/" in the HTML form code, then all the form data processing will be handled in the view "submitted()" which you'll have to make if you haven't already (hypothetical naming for ease of understanding).

  3. Like arulmr suggested, your code might look something like this in accordance to your example scenario:

    def test(request):
        return render(request, 'form.html')
    
    def submitted(request):
        if request.POST:
            #Do something with post data here
        return render_to_response('form_message.html')
    
  4. The more well known and "intuitive" method is to have a form submit to itself, rather than another view or URL.

    def test(request):
        if request.POST:
            #Do something with post data here
        return render_to_response('form_message.html')
    

    and...

    <form method="post" action=".">
    

The 'action="."' means to submit to itself.

Hope that adds clarity.

Tim Selaty Jr.
  • 599
  • 4
  • 6