I’m using django 5.1. After deploying it to my sharedhost using cpanel, static files not loading. I’m using “django.contrib.auth.middleware.LoginRequiredMiddleware”. When I remove this from Middleware it works. I need LoginRequiredMiddleware also I want to load static files. Please help me how can I do this. I’m using [How to Deploy your Django Web App and connect a MySQL Database in cPanel - DEV Community] to deploy my project.
I think there is not any static files configuration.
Try to search for static or media files page in your cPanel and customize for your project.
In inspect it shows http://ilife.sazzad.info.bd/login/?next=/static/vendor/fontawesome-free/css/all.min.css it should be http://ilife.sazzad.info.bd/login/static/vendor/fontawesome-free/css/all.min.css When I remove django.contrib.auth.middleware.LoginRequiredMiddleware then it works fine link shows http://ilife.sazzad.info.bd/login/static/vendor/fontawesome-free/css/all.min.css @newjasjanpython
Yeah. You are being redirected to login when you requested to your staticfiles. Can you show your urls.py in project folder.
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views
urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name='admin/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('admin/', admin.site.urls),
path('', include('core.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
this is my urls.py code @newjasjanpython
LOGIN_EXEMPT_URLS = [
'login',
STATIC_URL, MEDIA_URL, # others if you want
]
you can skip STATIC and MEDIA urls.
this is not working.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.LoginRequiredMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'core.middleware.CurrentUserAndIdleTimeoutMiddleware',
]
this is my middleware and
LOGIN_URL = '/login/' # This should match your login URL pattern
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
LOGIN_EXEMPT_URLS = [
'login',
STATIC_URL, MEDIA_URL, # others if you want
]
this my others settings.
Just delete middleware and use decorators.
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
@login_required
def my_view(request):
return render(request, 'my_template.html')
For class based views:
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView
class MyView(LoginRequiredMixin, TemplateView):
template_name = 'my_template.html'
This is best way for hosting.
(Use server for deployment)
In my opinion django.contrib.auth.middleware.LoginRequiredMiddleware
is redirecting your request for staticfiles to login page.