I'm starting learn Django and I stop on one thing - static files.
I tried like its below make changes in seetings and html but it doesnt load on the website. Please help me !
**settings.py:**
STATIC_ROOT = '/PycharmProjects/django_kurs/filmyweb/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = ['django_kurs/filmyweb/static/',]
**filmy.html:**
{% load static %}
<!doctype html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
<title>Document</title>
<link rel="stylesheet" href="{% static 'moj.css' %}">
</head>
Thank You in advance !
The changes to the settings is not sufficient, you need to add the handler for static files to the urls.py:
# urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# …
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Here we thus add views for the static and the media files (last line).
Note that Django does not serve static/media files on production. On production you typically configure nginx/apache/… to do this.
Related
I have tried everything suggested on the entire internet and stuck here for over 5 days
Here are my settings.py
STATIC_URL = '/static/'
# Add these new lines
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
I have a staticfile directory, i created with
python manage.py collectstatic
and I have all my css in that file
here are my urls
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('accounts.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Here are my app urls
from django.urls import path
from accounts.views import *
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', SignUpView.as_view(), name='signup'),
path('otp/', OTPView.as_view(), name='otp'),
path('login/', LoginView.as_view(), name='login'),
path('home/<int:id>', HomeView.as_view(), name="home"),
path('logout/', UserLogout.as_view(), name="logout"),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and here is my template base.html that I am inheriting in every other template
{% csrf_token %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/User_Authentication/staticfiles/admin/css/responsive.css">
</head>
I am getting this error
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:05:51] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:05:51] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:06:31] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Why am i still not able to use staticfiles
From the docs, static-files
{% load static %}
{% csrf_token %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'admin/css/responsive.css' %}">
</head>
You need to load it first, the order of loading these three might have to be altered.
You can run the app, go to the page, inspect and find out exactly what url django translates this into.
You are basically asking django/jinja to dynamically convert this static path to the corresponding static url instead of hardcoding it.
Note: {% load static %} needs to be added in every template file that uses "{% static '...' %}". Just adding it in base.html or an equivalent file that other templates extend from is not going to work.
I have deployed my web app on Microsoft IIS on my company server. Web.config file is set up and app is running with all permissions. I have created Virtual Directory (to enable serving static files map a static alias to the static directory, C:/inetpub/wwwroot/PyWeb/static/). No matter what I do I can't get my 'blog/main.css'. CSS is not loaded and I get error:
Refused to apply style from 'http://localhost/static/blog/main.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
base.html
{% 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, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="shortcut icon" href="/media/favicon.ico">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="{% static 'blog/main.css' %}">
Part href="/media/favicon.ico" is working and my icon is loaded. I have tried to remove rel="stylesheet" but it did not help.
Also, I have run collectstatic:
C:\inetpub\wwwroot\PyWeb>python manage.py collectstatic
Starting Scheduler...
You have requested to collect static files at the destination
location as specified in your settings:
C:\inetpub\wwwroot\PyWeb\static
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
1 static file copied to 'C:\inetpub\wwwroot\PyWeb\static', 128 unmodified.
I have added following to the settings.py but it did not help:
import mimetypes
mimetypes.add_type("text/css", ".css", True)
All static images from my Media folder are loaded with no problems, I'm just having an issue with my css file. CGI and Static Content is enabled in Windows Features.
You can try to add staticfiles_urlpatterns() in urls.py to your main application module
from .site import admin
from django.urls import path, include
from django.conf.urls import url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("auth2.urls")),
url(r'^ckeditor/', include('ckeditor_uploader.urls')),
]
urlpatterns += staticfiles_urlpatterns()
<meta charset="utf-8">
<title>Exam Page</title>
<link rel="stylesheet" href="../../../static/css/teststyle.css">
</head>
Im using this code in exam folder and i wanted to go to parent directory so i used ../../ but it is showing the below error
Not Found: /exam/static/css/teststyle.css
At the top of your template add {% load static %} and to add your css you can use {% static 'css/teststyle.css' %}.
{% load static %}
<meta charset="utf-8">
<title>Exam Page</title>
<link rel="stylesheet" href="{% static 'css/teststyle.css' %}">
</head>
In your main urls.py add
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
and in your settings.py add
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
I connected static files in django, but they won't connect, can you help?
settings:
STATICFILES_DIRS = [
"web_page/static",
]
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
index.html:
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="{% static "css/standart_template.css" %}" rel="stylesheet" type="text/css" >
<title>HouseVOP</title>
</head>
urls - projects:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('web_page.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urls - app:
from django.urls import path
from .views import FormListView, Success
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', FormListView, name = 'home'),
path('success/', Success, name = 'success')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Before turning here, I searched many sites, so now I may simply not see something. In the first version of the code, I laid the path to the static files along with os.path.join (BASE_DIR, etc., but it did not work ...
try this one:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
dont use web_page/static.no need to.
I created a simple project in Django but static files(CSS) not working.
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.portfolio),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
HTML file
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="{% static 'portfolio.css' %}">
</head>
<body>
<h1>hello world</h1>
</body>
</html>
picture of the project directory
blog is app and my_site is a project.
try adding
os.path.join(BASE_DIR, "my_site/static")
to STATICFILES_DIRS
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "my_site/static")
]
OR
place the static folder in the root my_site folder