django static files are not being found and loaded - python

Using django v2.2.1
I have a given project dir. structure:
In root project dir. there are some static files in static dir.
In base.html file,
{% load static %}
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- favicon -->
<link rel="shortcut icon" href="{% static 'favicon.ico' %}" />
<!-- Dropzone js -->
<link rel="stylesheet" href="{% static 'dropzone.css' %}">
<script src="{% static 'dropzone.js' %}" defer></script>
<!-- Custom js & css -->
<link rel="stylesheet" href="{% static 'style.css' %}">
.
.
This is settings.py file:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'sales', 'static'),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
In urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('sales.urls', namespace='sales')),
path('reports/', include('reports.urls', namespace='reports'))
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Also urlpatterns for sales
urlpatterns = [
path('', home_view, name='home'),
path('sales/', SaleListView.as_view(), name='list'),
path('sales/<pk>/', SaleDetailView.as_view(), name='detail'),
]
Tried
$ python manage.py findstatic style.css
No matching file found for 'style.css'.
Also I've been getting this error
I have restarted the server many times.

Adding the following to TEMPLATES in settings.py should help in your case:
For Django3.x
TEMPLATES = [
{
...
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
...
}
For Django2.x
import os
...
TEMPLATES = [
{
...
'DIRS': os.path.join(BASE_DIR, 'templates'),
'APP_DIRS': True,
...
}

Related

how to use static in django project

Is this not the correct way of making use of Django's static directories in code?
The folder is static/assets/css/style.css
settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
urls.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
base.html
{% load static %}
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="{% static 'assets/css/style.css' %}">
</head>
<body>
Go somewhere
</body>
</html>
style.css
a{
text-decoration: none;
}

how can I add a url for my template in django python?

i use a template im my django homepage.(http://127.0.0.1:8000/)
I linked statics files in static folder and formed like {% static .....}
when i path it in urls in the main page it work correctly.
but when I give it an slug like 'p/' it doesn't work.
because it search for statics in p folder:
"GET /p/static/post/js/scripts.js HTTP/1.1" 404 2598"
my app name is "post" .
please help?"
<link href="{% static 'post/css/bootstrap.css' %}" rel="stylesheet">
<link href="{% static 'post/css/fontawesome-all.css' %}" rel="stylesheet">
<link href="{% static 'post/css/swiper.css' %}" rel="stylesheet">
<link href="{% static 'post/css/magnific-popup.css' %}" rel="stylesheet">
<link href="{% static 'post/css/styles.css'%}" rel="stylesheet">
'
this my setting:
STATIC_URL = '/static/ MEDIA_URL = '/media/' STATIC_ROOT = os.path.join(BASE_DIR, "static") MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
this my urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('post.urls')),]'
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {
'document_root': settings.MEDIA_ROOT,
}),
]
this is my views:
'''
from django.shortcuts import render
from django.http import HttpResponse
from .models import app_post
def hello(request):
html = "<h>salam django<h>"
return HttpResponse(html)
def page(request):
return render(request,'post/index.html')
'''
this is my app urls:
urlpatterns = [
path('',page,name = 'page'),
path('h/',hello,name = 'hello'),
]
Add this in your settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
and then run command python manage.py collectstatic

Django static files does not load while in the root directory

I stored my static files in project root directory but I can't make them load on the browser.
configurations
settings.py
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIR = [
STATIC_DIR,
]
template file
<!DOCTYPE html>
{% load static %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Index</title>
</head>
<body>
<h1>{{temp}}</h1>
<h2>Click here to veiw user details</h2>
<img src="{% static "user/images/smile.png"%}" alt="oh oh didn't show"/>
</body>
</html>
In the file urls.py add the static url.
urlpatterns = [
path('admin/', admin.site.urls),
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Why will the css in my django project not render?

I'm still just playing around with Django and cannot figure out what I am doing wrong with my CSS file. My HTML works but not the CSS. I'm guessing it has something to do with settings.py or some special "Django code" I'm not using. Here is the code for my HTML (what I am guessing is the relevant code to show).
Here is my HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>home</title>
<link rel="stylesheet" type="text/css" href="../css/styles.css"/>
</head>
<body>
<h3>HELLO WORLD!</h3>
</body>
</html>
Here is my settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'ecommerce005.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Here is my urls.py:
from django.conf.urls import url
from django.contrib import admin
from .views import home_page
urlpatterns = [
url(r'^$', home_page),
url(r'^admin/', admin.site.urls),
]
Here is my views.py:
from django.http import HttpResponse
from django.shortcuts import render
def home_page(request):
return render(request, "index.html", {})
The order of my file are as follows:
'src' directory:
1) 'css' folder holds my styles.css file
2) 'projectname' folder contains all of my django files shown above
3) 'templates' folder holds my index.html file
Please be as specific as possible. I found sources online but they seemed too vague for me to find a solution. I appreciate your help.
Configure static files in settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS=[
(os.path.dirname(BASE_DIR),"static"),
]
STATIC_ROOT = (os.path.dirname(BASE_DIR),"static_files")
In Templates,(ie HTML file)
{% load static %}
<html>
....
<link href="{% static 'styles/style.css' %}" rel="stylesheet" media="screen, print" type="text/css">
Directory Structure
-ProjectFolder
--|static
----|styles
------|styles.css
You need to setup a 'STATIC_ROOT' in the settings.py
STATIC_URL = '/staticfiles/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
If DEBUG=False, you need to setup your proxy server such as nginx to serve these staticfiles
This link can explain it further.
Wootton, as was said, you need to setup your static_root on settings.py, after that you can just call your css
<link type="text/css" rel="stylesheet" href="{{ STATIC_URL }}css/style-responsive.css">
but before/after setup the static_root you need to create a folder named static and serve your css in there.
I think this will help you!

TemplateDoesNotExist. loading static files and templates Django project structure

I am a bit confused on how to yield templates from other apps
Here is a picture of my project structure:
I am getting this error:
TemplateDoesNotExist at /
home.html
home/views.py:
class HomeView(generic.TemplateView):
template_name = "home.html"
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "home/static/home/js"),
os.path.join(BASE_DIR, "home/static/home/images"),
os.path.join(BASE_DIR, "home/static/home/css"),
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
project/home/urls.py:
from django.conf.urls import patterns, url
from home.views import HomeView
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"),
)
project/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include("home.urls", namespace="home")),
)
project/templates/home/index.html
<!DOCTYPE html>
<html>
<head lang="en">
...
<!-- Fonts -->
<link href='http://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>
<!-- Custom CSS -->
<link href="{{ STATIC_URL }}boothie.css" rel="stylesheet">
<!-- Custom JS-->
<script src="{{ STATIC_URL }}boothie.js"></script>
<title>Boothie</title>
</head>
<body>
<div class="container">
...
</div>
</head>
{% block content %}{% endblock %}
</body>
</html>
project/home/templates/home/home.html:
{% extends index.html %}
{% block content %}
...
<script src="{{ STATIC_URL }}js/jquery.easing.1.3.js"></script>
<script src="{{ STATIC_URL }}js/boothie.js"></script>
{% endblock %}
Django stops at the first template that it finds matching the requested filename, and it will start at the templates directory in the app folder.
If it doesn't find it here, it will then go up the chain till it has searched all locations in the TEMPLATE_DIRS setting, and only then will it print an error that it cannot find a template. The way it searches for templates is defined by the TEMPLATE_LOADERS setting.
In your project layout, the template you want is located in the template directory of the home app, and inside this directory, under the home directory. Thus the template path should be home/home.html in your view:
class HomeView(generic.TemplateView):
template_name = "home/home.html"
If your templates directory is in your project's root you have to add it to setitngs.py
If it's the subdirectory of an app (that is in INSTALLED_APPS in settings.py) then the app template loader (enabled by default) will load it.
If your app is called home then you would have the templates dir inside the home dir, not home under templates which would make it accessible as home/home.html since it's under the home subfolder of the templates dir in settings.py

Categories

Resources