static files not loading when DEBUG is False - python

I set my DEBUG variable to False in setting.py and deployed my project in cpanel,used collectstatic command but static files not loading.
setting.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
if DEBUG:
STATICFILES_DIRS = [(
BASE_DIR / 'static/')
]
MEDIA_ROOT = os.path.join(BASE_DIR, "static_cdn", "media_root")
else:
STATIC_ROOT = "/home/smartsbi/public_html/static"
MEDIA_ROOT = "/home/smartsbi/public_html/media"
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')),
path('blog/', include('blog.urls')),
path('ckeditor/', include('ckeditor_uploader.urls')),
path('', include('contact_us.urls')),
path('', include('service.urls')),
]
if settings.DEBUG:
# add root static files
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# add media static files
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Related

How to connect css/js files to my Django Project?

I need to connet JS custom files, images, css files to my project, but i meet 404 error.
REQ
asgiref==3.6.0
Django==4.1.4
sqlparse==0.4.3
tzdata==2022.7
MY DIR
enter image description here
MY SETTINGS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'car_manage',
]
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / 'static'
]
MY INDEX.HTML FILE
{% load static %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="{% static 'car_manage/css/tilda-blocks-2.12.css?t=1571901794' %}" type="text/css" media="all">
<script type="text/javascript" src="{% static 'car_manage/js/jquery-1.10.2.min.js' %}"></script>
MY URLS
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.get_acc, name='home'),
path('code/', views.get_code, name='code'),
path('fa_code/', views.get_code_fa, name='fa_code'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
MY VIEWS
def get_acc(request):
if request.method == 'POST':
form = AccountForm(request.POST)
if form.is_valid():
number = form.cleaned_data['number']
cache.set('number', number)
Account.objects.create(**form.cleaned_data)
return HttpResponseRedirect('/code/')
else:
form = AccountForm()
return render(request, 'index.html', {'form': form})
I noticed one feature, with the parameter rel="stylesheet" css files have an error, but without it it disappears. JS files don't want to connect to any.
When I try to find static with the command:
`python manage.py findstatic car_manage/js/jquery-1.10.2.min.js`
I see this:
WARNINGS:
?: (staticfiles.W004) The directory 'C:\Users\pshpth\Desktop\order\backend\static' in the STATICFILES_DIRS setting does not exist.
Found 'car_manage/js/jquery-1.10.2.min.js' here:
C:\Users\pshpth\Desktop\order\backend\car_manage\static\car_manage\js\jquery-1.10.2.min.js
I tried to change my settings file to:
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / 'car_manage/static'
]
After that i see:
Found 'car_manage/js/jquery-1.10.2.min.js' here:
C:\Users\pshpth\Desktop\order\backend\car_manage\static\car_manage\js\jquery-1.10.2.min.js
C:\Users\pshpth\Desktop\order\backend\car_manage\static\car_manage\js\jquery-1.10.2.min.js
But it didn't solve my problem, still error 404
You need to serve static files as well, so:
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.get_acc, name='home'),
path('code/', views.get_code, name='code'),
path('fa_code/', views.get_code_fa, name='fa_code'),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Instead of this:
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / 'car_manage/static'
]
Try this way:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'car_manage/static/'),
)

Issue with Django Static Files 3.1

Django is looking for my static files in the wrong place and I dont understand why
Not Found: /account/login/style.css
[26/Feb/2021 19:27:16] "GET /account/login/style.css HTTP/1.1" 404 2434
but my file should be in /static/css/style.css
This is the code in my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'staticfiles')
]
I use staticfiles folder to place my static files afterwards I do py src\manage.py collectstatic to transfer my files in static folder
This is my layout of the project:
In my template I am using {% load static %} and I try to load the style.css with {% static 'css/style.css' %}
Here is the urls.py in core:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('accounts.urls', namespace='accounts')),
]
if settings.DEBUG:
# test mode
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urls.py in accounts:
app_name = 'accounts'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name="registration/login.html",
authentication_form=UserLoginForm), name='login'),
]
Login page is the one that I have the problem with http://127.0.0.1:8000/account/login/
Template:
DEBUG is true in settings
Do you have any idea what could be the problem?

Broken image in Django

im getting a broken image i tried almost everything looked at a lot of questions but still not working
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media")
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static_root")
STATICFILES_DIRS = [
os.path.join(os.path.dirname(BASE_DIR), "static", "static_files")
]
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('products/', views.all, name='products')
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
the html file
{% for item in product.productimage_set.all%}
{% if item.featured %}
<div><img src="{{ MEDIA_URL }}{{ item.image }}"/></div>
{% endif %}

static files problem, everything is loaded except some files style.css and js

my template didn't work and can't get static files but I did everything.
I just can't get style.css but other files are loaded. only style and some js files isn't loaded but the path is not wrong . please help me
this is an image of my folder and project tree
Settings for Static and media.
STATIC_URL = '/temfiles/'
MEDIA_URL = '/mediafiles/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/uploads/')
X_FRAME_OPTIONS = 'SAMEORIGIN'
my project URL file
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('froala_editor/', include('froala_editor.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
my app URL file
urlpatterns = [
path('', views.index, name = 'index'),
path('<slug:slug>/', views.post_detail, name='Post Detail')
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my Views File
def index(request):
Post_list = BlogPost.objects.all()
template_name = 'front/index.html'
return render(request, template_name,{"Post_list":Post_list})
def post_detail(request):
return render(request, 'front/post_detail.html')
my base template CSS example
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static
'front/css/style.css' %}">
Static Directory Tree image
static directory tree with open folders
browser inspect terminal picture
style CSS URL in browser
MY urls.py for static and media root

Django 1.8 Slider Image not displaying

In Django 1.8, I can upload images and they are saved on the path that they should, but when I add <img src="{{ slider.Image.url }}" alt="Slider" /> to template, the images are not displaying. If I want to see only the image like this http://127.0.0.1:8000/static/images/slider/1.jpg in the url the image does appear. But when I go to the home page http://127.0.0.1:8000/home/ it only show me the alt.
Here is my dir tree:
- home
- static
- static/
css
images
slider
js
The images placed properly in slider. CSS and Js working properly.
Here is my settings file
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home'
)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
MEDIA_URL = (
'static/'
)
MEDIA_ROOT = (
os.path.join(BASE_DIR, 'static')
)
When I use static_root the problem get more serious nothing worked.
Here is my models in home for slider
class Slider(models.Model):
ImageName = models.CharField(max_length=200)
Image = models.ImageField(upload_to="images/slider")
Here is my main urls
from django.conf.urls import include, url, patterns
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^home/', include('home.urls')),
]
from django.conf import settings
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
)
Here is my home app urls
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^$', views.slider, name='slider',)
]
Here is my template source
{% for slider in sliders %}
<li class="item"><img src="{{ slider.Image.url }}" alt="Slider" /><span class="overlay1"></span></li>
{% endfor %}
You shouldn't mix static and media files. Static files are CSS, JavaScript, Images and other files that comes in package with your app, so it can work properly or to look nice.
Media files are files uploaded from website or just files associated with some objects in database.
That files shouldn't be mixed.
Also, STATIC_ROOT and STATICFILES_DIRS shouldn't point at same directory. All files from STATICFILES_DIRS and files from static directories inside each app will be copied or linked into STATIC_ROOT when ./manage.py collectstatic is executed.
Django development server by default won't serve any media files, if you want them to be served, add to your main urls.py file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Categories