chobo
chobo

Reputation: 4862

How do I use request.META.get('HTTP_REFERER') within template?

I'd like to use request.META.get('HTTP_REFERER') within template.

My template source:

<!-- this is login.html -->
{% extends "base.html" %}
{% block title %}django bookmark- login{% endblock %}
{% block head %}login{% endblock %}
{% block content %}
    {% if form.errors %}
    <p>try again!</p>
    {% endif %}
    <form method="post" action=".">{% csrf_token %}
        <p><label for="id_username">username:</label>
        {{ form.username }}</p>
        <p><label for="id_password">password:</label>
        {{ form.password }}</p>
        <input type="hidden" name="next" value="/<!-- I WANT TO USE 'HTTP_REFERER' HERE -->" />
        <input type="submit" value="login" />
    </form>
{% endblock %}

How what should I do?

urlpatterns = patterns('', (r'^login/$', 'django.contrib.auth.views.login'),

Upvotes: 13

Views: 30138

Answers (3)

oz123
oz123

Reputation: 28858

Actually the preferred way is to use the next parameter as documented here

You can do in your template something like this:

<input type="hidden" name="next" value="{{  request.GET.next }}" />

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599590

There's no need for get.request.META is a dictionary, and as with all dictionaries, you can perform field lookup in the template using the dot notation: {{ request.META.HTTP_REFERER }}

Upvotes: 17

Aamir Rind
Aamir Rind

Reputation: 39659

Add django.core.context_processors.request in your settings file in TEMPLATE_CONTEXT_PROCESSORS then you would be able to use the request in template without explicitly passing it in request context.

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.request', # this one
)

the in template you could do {{request.META.HTTP_REFERER}}

Upvotes: 6

Related Questions