How can i require the user to login to see the view ?

12 views
Skip to first unread message

Mostafa Alaa

unread,
Jul 23, 2018, 7:45:16 PM7/23/18
to Django users
the view of  app 

from django.shortcuts import render
from .models import Article
from django.contrib.auth.decorators import login_required
# Create your views here.
def index(request):
articles = Article.objects.all()
return render(request,'app/index.html',{'articles':articles})



def detail(request,slug):
articles = Article.objects.get(slug=slug)
return render(request, 'app/detail.html', {'articles':articles})



@login_required
def create_article(request):
return render(request,'app/create.html')






the settings of login
LOGIN_REDIRECT_URL = '/app/index/'
LOGIN_URL = '/accounts/login/?next=/'


mottaz hejaze

unread,
Jul 24, 2018, 12:13:30 AM7/24/18
to django...@googlegroups.com

The login_required decorator

login_required(redirect_field_name='next'login_url=None)[source]

As a shortcut, you can use the convenient login_required()decorator:

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a169e289-8fa7-4bb2-971e-c75b4978474b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages