Monday, July 23, 2018

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

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):      ...

On Tue, 24 Jul 2018, 01:44 Mostafa Alaa, <anamasry49@gmail.com> wrote:
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=/'


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAHV4E-dhSYB8BXF%3Dz8dExMF3SPqt9a4u3sy0j30ai19KQ7Z96Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment