django static files 404 error after migration - django

static folder path correct and it was all working before migration.
index.html
{%load staticfiles%}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="{% static "css/mycss.css"%}"/>
<title>My first Django App</title>
</head>
<body>
<h1>{{somthin}}</h1>
<img src="{% static 'images/zoro.jpg'%}" alt="Oops!No Image">
</body>
</html>
after python manage.py runserver in terminal static files are showing 404 error.

make sure all these configuration you have in your settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
and in main urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and use {% load static %} tag in templates, also if you use double quote outside curly brackets, use single quote inside
<link rel="stylesheet" href="{% static 'css/mycss.css' %}"/>

Related

Django - Static file, image, wont appear

I'm learning django. It was working in the beginning of my project, but now imgs from static just dont appear.
I've try to run collecstatic, work with STATIC_ROOT, other images and so on.
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_URL = '/upload/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload')
Home.html
{% load static %}
<img scr="{% static 'img/cart-icon.png' %}">
Files dir
{% load static %}
<img src="{% static 'img/cart-icon.png' %}">
src not scr

load picture from static file django

Django is not loading my static files. I'm trying to load an image from the static folder, but it doesn't work.
file.html
<body>
{% load static %}
<img src="{% static "products/logo.jpg" %}" alt="My image">
</body>
structure
app
|_______products
|_________templates
|_________file.html
|_________models.py
|_________ ...
|______static
|_________products
|_________logo.jpg
settings
...
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
...
]
...
STATIC_URL = '/static/'
You need to put this in your settings.py file:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
<!DOCTYPE html>
<html>
<head>
{% load static %}
</head>
<body>
<img src="{% static "products/logo.jpg" %}" alt="My image">
</body>
</html>
settings:
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]

static image/media files in django

i am unable to load static files with django in my template my files are shown below
Settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Theme'
]
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIR = [
STATIC_DIR,
]
Template/index.html
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First App</title>
</head>
<body>
<h1>Hello this is index.html!</h1>
<img src="{% static 'images/mark.jpg' %}" alt="My image">
</body>
</html>
project Direstory
This is the projec t directory`
Add these code also in your main url file
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and in setting add this line too
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
STATICFILES_DIR list should be named STATICFILES_DIRS.
In development when you run with runserver and DEBUG=True it is enough.
To serve staticfiles in production:
usage of external web-server (in production you will most probably have one or web-proxy in front of django) is recommended. Call collectstatic
and serve the resulted static_root dir with web-server
Or you can upload them to CDN (AWS S3, CLoudflare)
Or it is possible to make django serve them using Whitenoise
least recommended but still possible - serving with django as is development by adding static urls

django-compressor CommandError: An error occurred during rendering file/path.html: Invalid class path 'css'

I'm trying to use django-compressor for my project. So far in my development environment, when I run python manage.py compress I been get this error CommandError: An error occurred during rendering D:path/to/a/template.html: Invalid class path 'css'
I have lots of template files spread across many apps. The error pops up randomly, pointing to a different template each time I run python manage.py compress.
I have also tried first running python manage.py collectstatic, but to no avail and nothing in the docs seem to mention this error
OS: Windows 10, Django==2.0.5, django-compressor==2.2
Relevant settings.py section below
DEBUG = True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_URL = STATIC_URL
COMPRESS_PARSER = 'compressor.parser.HtmlParser'
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
COMPRESS_OFFLINE_MANIFEST = 'compressor_manifest.json'
COMPRESS_CSS_FILTERS = {
'css': ['compressor.filters.css_default.CssAbsoluteFilter'],
'js': ['compressor.filters.jsmin.JSMinFilter']
}
COMPRESS_PRECOMPILERS = (
('text/x-scss', 'django_libsass.SassCompiler'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
Inside the <head></head> tag of my project's base.html template I have
{% load compress %}
{% load staticfiles %}
{% load static %}
{% compress css file %}
<link rel="stylesheet" href="{% static 'css/fname.css' %}">
{% endcompress %}
{% compress js file %}
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
<script type="text/javascript" src="{% static 'js/fname.js' %}"></script>
{% endcompress %}
You must define COMPRESS_FILTERS instead of COMPRESS_CSS_FILTERS as the latter is only a backwards-compatible alias for the css key of the former. That is, your configuration should read:
COMPRESS_FILTERS = {
'css': ['compressor.filters.css_default.CssAbsoluteFilter'],
'js': ['compressor.filters.jsmin.JSMinFilter']
}

Django Bootstrap Integration

Well I'm trying to use Bootstrap with Django by copying and pasting the folders from Bootstrap to static folder. But I'm not able to get access to the Bootstrap.css file. It shows 404 error.
Just for experimenting purpose, I created a main.css in the static/css folder and linked it in the template and to my surprise, its running.
So, why the bootstrap.css file is not getting linked.
Here is how I'm linking Bootstrap.css in my template
{% block css %}
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.css">
{% endblock %}
Here is my root urls.py
urlpatterns = patterns('',
(r'^',include('apps.home.urls')),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':os.path.normpath( os.path.join( os.path.dirname(__file__),'../static/'))}),
url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
MEDIA_ROOT = 'C:/Users/Praful/uploads'
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
You should try this instead:
{% load static %}
{% block css %}
<link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.css ' %}">
{% endblock %}
...and just make sure you have 'django.contrib.staticfiles' in your INSTALLED_APPS. Please see also https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#template-tags
(EDIT: fixed typo, I oroginally forgot the "static" keyword)
OR this:
{% block css %}
<link rel="stylesheet" type="text/css" href="{{STATIC_URL}}css/bootstrap.css">
{% endblock %}
..assuming "django.core.context_processors.media" is in your TEMPLATE_CONTEXT_PROCESSORS
Take a look at the faststart-bootstrap Django project skeleton with Bootstrap 3 support. However it's based on LESS style sheets, but LESS is easy to learn (in use) and compatible with CSS.
ok, that's a settings and path problem.
First, put that in your settings:
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__))
MEDIA_ROOT = os.join(PROJECT_PATH, 'uploads')
MEDIA_URL = '/media/'
STATIC_ROOT = os.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'
And that in your template
{% block css %}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/css/bootstrap.css">
{% endblock %}
Check your project structure. I follow this structure:
/Project_name
/App1
/App2
/App3
/Project_name
/media
/static
/css
bootstrap.min.css
/js
/templates
/Requirements
requirement.txt
.gitignore
With this structure and a proper settings and template files shown by lalo, bootstrap should be work without 404.