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()
Related
<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'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.
I am learning Django and while I was trying to load static CSS files, it did not show any output. When I run server I only get HTML as my output.
Here is my code
settings.py code
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
'bloodnepal/bloodnepal/static',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
{% load static %}
<meta charset="utf-8">
<title>Bloodnepal</title>
<link rel="stylesheet" href="{% static 'css_files/style.css' %}">
<link rel="stylesheet" href='{% static "css_files/link.css" %}'>
<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Fira Sans Extra Condensed' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Nanum Myeongjo' rel='stylesheet' type='text/css'>
</head>
</html>
url.py
from django.contrib import admin
from django.urls import path
from . import views #imported the views.py files here for our pipeline
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('home/',views.home,name="Homepage"),
path('donate/',views.donate,name="Donatepage"),
path('organization/',views.organization,name="Organizationpage"),
path('getinvolved/',views.getinvolved,name="Getinvolvedpage"),
path('about/',views.about,name="AboutUspage"),
]
urlpatterns += staticfiles_urlpatterns()
If you store your CSS inside your project folder under "static" directory then the following code for settings.py should work:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
....
STATIC_URL = '/static/'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
]
I have created a project in Django. Also, I am using django-allauth for sign up and login.
In order to use my own templates with django-allauth, I have created a html file called signup.html in a folder called account inside a folder called templates which is outside of of all my apps (/templates/account/signup.html). That works.
I tried to use some custom css file inside signup.html:
<link rel="stylesheet" href="/templates/account/signup.css">
It says that the file can not be found. Though, it is located in templates/account.
your css file must under STATICFILES_DIRS in settings.py,set this in settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
and put account/signup.css file to /static/account/signup.css,you can get it like:
<link rel="stylesheet" href="/static/account/signup.css">
or
{% load static %}
<link rel="stylesheet" href="{% static 'account/signup.css' %}">
I have read many posts about the topic and tried to follow all the suggestions (new to python here) but I am unable to get bootstrap to work in Django.
I have a project "myoffice" and an app "proposals" in it.
I have downloaded a css file and put it in following folder
django\bin\myoffice\proposals\static\bootstrap\bootstrap.min.css
I have made following changes to the settings.py
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATICFILES_DIRS = (
os.path.join(
os.path.dirname(__file__),
'static',
)
STATIC_URL = 'proposals/static/'
and included static files in my template like this.
<!DOCTYPE html>
<html>
<head>
{% load staticfiles %}
<link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet" media="screen">
</head>
<body>
But it is still showing view without any formatting.
I figured it out. It was actually a stupid error. I discovered, I have to restart the webserver after adding css file to the static directory and making changes to the settings.py