简体   繁体   中英

How to access & modify Django template context variables

In my urls.py, I map the url accounts/login/ to the login module, with a reference to the template i want to use:

url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'templates/login.html'})

This works fine, but I want to change the value of the next property, which specified where the user ought to be redirected after a successful login. How can I access these variables and print their values, and more importantly, how can I modify them?

Thanks!

Direct the url to a view:

(r'^accounts/login/', 'myproj.login.views.mylogin')

Then handle redirection in your view code:

def mylogin(request, **kwargs):
    if request.user.is_authenticated():
        if 'next_url' in request.session:
            url = request.session['next_url']
            del request.session['next_url']  # Cleaning next_url val
            return HttpResponseRedirect('/%s' % url)
        else:
            return HttpResponseRedirect('/')
    return login(request, **kwargs)

@csrf_protect
def login(request, template_name='registration/login.html'):
    """Displays the login form and handles the login action."""
    retval = django.contrib.auth.views.login(request, template_name)
    clear_session_data(request.session)
    return retval

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM