user1050619
user1050619

Reputation: 20916

Accessing request variable in template

Im writing a simple application, where I can collect some information from a form and display it on the same template..

Below is the code,

When I try to access - {{request.POST['fname']}} - from the template , I get the following error,

Could not parse the remainder: '['fname']' from 'request.POST['fname']'

How do I access the request variable in the template?

views.py

    def test_page(request):
        print 'request.method =', request.method
        if request.method == 'POST':
            print 'request.post = ', request.POST['fname']
            variables = RequestContext(request,{'display_form':False})
            return render_to_response('test_page.html',variables)
        else:
            variables = RequestContext(request,{'display_form':True})
            return render_to_response('test_page.html',variables)

template

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Test Page</title>
    </head>
    <body>
    This is a test page
    {% if display_form %}
        <form action="." method="post">{% csrf_token %}
            FIRST NAME : <input type="text" name="fname">
            <input type="submit" value="register"/>
        </form>
    {% else %}
        {{request.POST['fname']}}
    {% endif %}

    </body>
    </html>

Upvotes: 1

Views: 163

Answers (4)

nickzam
nickzam

Reputation: 813

It's a bad idea to get request.POST explicitly in template. Handling request.POST is more business logic related so controller/view should handle it.

Here is optimized code. There are less code duplication and it's more clear. fname is None if there are no first name specified. It's also better to use Django Form or ModelForm class to handle forms. Please check it here: https://docs.djangoproject.com/en/dev/topics/forms/

You should also use UTF-8 instead of charset=ISO-8859-1" to have less problems with unicode characters later.http-equivis a little bit outdated (it's used for HTML 4.01), usemeta charset` instead.

view:

def test_page(request):

    if request.method == 'POST':
        fname = request.POST.get('fname')
        display_form = False
    else:
        fname = None
        display_form = True

    variables = RequestContext(request,{'display_form': display_form, 'fname': fname})
    return render_to_response('test_page.html', variables)

template:

    <html>
<head>
<meta charset="UTF-8">
<title>Test Page</title>
</head>
<body>
This is a test page
{% if display_form %}
    <form action="." method="post">{% csrf_token %}
        FIRST NAME : <input type="text" name="fname">
        <input type="submit" value="register"/>
    </form>
{% else %}
    {{ fname }}
{% endif %}

</body>
</html>

Upvotes: 1

Henrik Andersson
Henrik Andersson

Reputation: 47222

First off you should index it via request.POST.fname it's a dictionary like object and also it always returns strings, just a FYI.

Secondly, after dealing with a POST request you should always issue a redirect (HttpResponseRedirect) and not a normal render. This will prevent data from being submitted twice if the user presses the back button.

This is best practice and should always be done.

Upvotes: 0

Simeon Visser
Simeon Visser

Reputation: 122536

You can write:

{{ request.POST.fname }}

This will perform a lookup of fname in request.POST.

Django supports:

  • dictionary lookups (foo['bar'])
  • attribute lookups (foo.bar)
  • list lookups (foo[bar])

whenever you write foo.bar (see Variables and lookups in the documentation).

Upvotes: 2

Alex Parakhnevich
Alex Parakhnevich

Reputation: 5172

The proper way to access a dictionary value in template is: {{ request.POST.fname }}

Upvotes: 1

Related Questions