Django Tutorial - Video 9 - User Profile and Login_Required decorator

This tutorial covers the get_profile() and the @login_required decorator


Add LOGIN_URL and LOGIN_REDIRECT_URL to settings.py

# URL for @login_required decorator to use
LOGIN_URL = '/login/'

# redirect authenticated users
LOGIN_REDIRECT_URL = '/profile/'
		

You can now use @login_required in your views

add Profile function to drinker/views.py

@login_required
def Profile(request):
        if not request.user.is_authenticated():
                return HrttpResponseRedirect('/login/')
        drinker = request.user.get_profile
        context = {'drinker': drinker}
        return render_to_response('profile.html', context, context_instance=RequestContext(request))
		

templates/profile.html

{% extends "base.html" %}
{% block content %}
        <p>Name: {{ drinker.name }}</p>
        <p>Birthday: {{ drinker.birthday }}</p>
{% endblock %}
		

Add URL hook for /profile/ in urls.py

    (r'^profile/$', 'drinker.views.Profile'),