|
| 1 | +title: django.contrib.auth.decorators login_required Example Python Code |
| 2 | +category: page |
| 3 | +slug: django-contrib-auth-decorators-login-required-examples |
| 4 | +sortorder: 50025 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.contrib.auth.decorators login_required |
| 7 | +meta: Python code examples for the Django function login_required from the django.contrib.auth.decorators module. |
| 8 | + |
| 9 | + |
| 10 | +[Django](/django.html)'s |
| 11 | +[login_required](https://docs.djangoproject.com/en/dev/topics/auth/default/#the-login-required-decorator) |
| 12 | +function is used to secure views in your web applications by forcing |
| 13 | +the client to authenticate with a valid logged-in User. This decorator |
| 14 | +is a handy shortcut that can reduce the amount of code in your view |
| 15 | +functions and eliminate the need for every function to have |
| 16 | +boilerplate like `if not request.user.is_authenticated:`. |
| 17 | + |
| 18 | + |
| 19 | +## Example 1 from dccnsys |
| 20 | +[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration |
| 21 | +web app built in [Django](/django.html). The code is open source under the |
| 22 | +[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE). |
| 23 | + |
| 24 | +[**dccnsys / wwwdccn / registration / views.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/registration/views.py) |
| 25 | + |
| 26 | +```python |
| 27 | +from django.contrib.auth import get_user_model |
| 28 | +~~from django.contrib.auth.decorators import login_required |
| 29 | +from django.shortcuts import render, redirect |
| 30 | + |
| 31 | +from users.models import generate_avatar |
| 32 | +from users.forms import (PersonalForm, ProfessionalForm, |
| 33 | + SubscriptionsForm) |
| 34 | + |
| 35 | +User = get_user_model() |
| 36 | + |
| 37 | + |
| 38 | +~~@login_required |
| 39 | +def personal(request): |
| 40 | + profile = request.user.profile |
| 41 | + if request.method == 'POST': |
| 42 | + form = PersonalForm(request.POST, instance=profile) |
| 43 | + if form.is_valid(): |
| 44 | + form.save() |
| 45 | + profile.avatar = generate_avatar(profile) |
| 46 | + profile.save() |
| 47 | + return redirect('register-professional') |
| 48 | + else: |
| 49 | + form = PersonalForm(instance=profile) |
| 50 | + return render(request, 'registration/personal.html', { |
| 51 | + 'form': form |
| 52 | + }) |
| 53 | + |
| 54 | + |
| 55 | +~~@login_required |
| 56 | +def professional(request): |
| 57 | + profile = request.user.profile |
| 58 | + if request.method == 'POST': |
| 59 | + form = ProfessionalForm(request.POST, instance=profile) |
| 60 | + if form.is_valid(): |
| 61 | + form.save() |
| 62 | + return redirect('register-subscriptions') |
| 63 | + else: |
| 64 | + form = ProfessionalForm(instance=profile) |
| 65 | + return render(request, 'registration/professional.html', { |
| 66 | + 'form': form |
| 67 | + }) |
| 68 | + |
| 69 | + |
| 70 | +~~@login_required |
| 71 | +def subscriptions(request): |
| 72 | + subscriptions = request.user.subscriptions |
| 73 | + if request.method == 'POST': |
| 74 | + form = SubscriptionsForm(request.POST, instance=subscriptions) |
| 75 | + if form.is_valid(): |
| 76 | + form.save() |
| 77 | + request.user.has_finished_registration = True |
| 78 | + request.user.save() |
| 79 | + return redirect('home') |
| 80 | + else: |
| 81 | + form = SubscriptionsForm(instance=subscriptions) |
| 82 | + return render(request, 'registration/subscriptions.html', { |
| 83 | + 'form': form |
| 84 | + }) |
| 85 | +``` |
0 commit comments