0

I want to make the login page the first page appear to the user what should I write in the path?

urlpatterns = [
    # path('', views.home, name='home')
    #path('', HomeView.as_view(), name='home'),
    path('', ??? ,name='login'),# what I should I put here to make login page the first page appear
    path('like/<int:pk>',LikeView,name='like_post'),
    path('article/<int:pk>', ArticleDetailView.as_view(), name='article-details'),
    path('add_post/', AddPostView.as_view(), name='add_post'),
    path('add_category/', AddCategoryView.as_view(), name='add_category'),
    path('article/edit/<int:pk>', UpdatePostView.as_view(), name='update_post'),
    path('article/<int:pk>/remove', DeletePostView.as_view(), name='delete_post'),
    path('category/<str:cats>/', CategoryView, name='category'),
    path('category-list/', CategoryListView, name='category-list'),
    
]
Stefano
  • 128
  • 1
  • 9

4 Answers4

2

So you want to redirect unauthenticated users to Login page
You can use this MiddleWare:

from django.http import HttpResponseRedirect
from django.urls import reverse


class AuthRequiredMiddleware(object):
    def process_request(self, request):
        if not request.user.is_authenticated():
            return HttpResponseRedirect(reverse('login')) # Your login page url
        return None  

And in your settings.py you need to add your MiddleWare:

MIDDLEWARE_CLASSES = (
    ...
    'path.to.your.AuthRequiredMiddleware',
)
Mojtaba Arezoomand
  • 2,140
  • 8
  • 23
1

If you need the user to login before accessing any page, you can use a custom middleware. See the following:

If you only need the user to login before accessing certain pages, you can use:

1

I don't thinks it's the best way, but it's the easiest way to acomplish that. On your html template just add this:

{% if user.is_authenticated %}

<body>
...
</body>

{% else %}

<script>
    window.location.href = '/login' // Your login page URL
</script>

{% endif %}
Matias Coco
  • 351
  • 3
  • 9
0

thanks, I got it here is the solution

def LoginView(request):
    return HttpResponseRedirect(reverse('login'))
path('', LoginView ,name='login'),
Stefano
  • 128
  • 1
  • 9