Django project not recognizing SCSS files - python

I've been struggling to get Django to recognize my SCSS files (I may be misusing SCSS/SASS/LESS terminology... their relationship confuses me). I'm using django-libsass and compress, both of which seem pretty straight-forward. My page is giving me the error "Resource interpreted as Stylesheet but transferred with MIME type application/octet-stream: ". It's loading the page but none of my styles are showing.
I'm not sure what people need to see. My template includes:
<link href="{% static 'css/blog.scss' %}" rel="stylesheet">
(this worked fine when it was css)
Settings:
COMPRESS_PRECOMPILERS = (
('text/x-scss', 'django_libsass.SassCompiler'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# other finders..
'compressor.finders.CompressorFinder',
)

Try adding type="text/x-scss" to the link element along with the compress tag:
{% compress css %}
<link href="{% static 'css/blog.scss' %}" rel="stylesheet" type="text/x-scss">
{% endcompress %}
And make sure to load the compress tag before you use it:
{% load compress %}

Related

Static files not loading with Django. (Bootstrap not working)

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

Django static templatetag not displaying SVG

I'm trying to use django's static templatetag to display an SVG, but it doesn't seem to recognize the SVG as a valid image url. This is what I currently have:
settings.py
import mimetypes
mimetypes.add_type("images/svg+xml", ".svg", True)
landing.html
{% load staticfiles %}
<img src="{% static 'images/right-arrow.svg' %}" />
At least in my view.py, it recognizes the SVG mimetype:
views.py
print(mimetypes.guess_type(static('images/right-arrow.svg')))
# returns ('images/svg+xml', None)
The SVG does display in a non-django page, and it will download the SVG if I try to open the SVG path in a new browser tab.
I'm currently using python 3.4 and django 1.8.4.
I found the issue. In settings.py, it should be mimetypes.add_type('image/svg+xml', '.svg', True). image should be singular.
I faced a similar issue.I would recommend you to use :
src="{{ STATIC_URL }} images/right-arrow.svg" instead of src="{% static 'images/right-arrow.svg' %}"
svg format might not always identify django's method of obtaining staticfile contents.Hope this helps :)
Add this in your settings.py file.
import mimetypes
mimetypes.add_type("image/svg+xml", ".svg", True)
mimetypes.add_type("image/svg+xml", ".svgz", True)
In your cases you have added images in add_type which should be singular (image).
You are loading staticfiles and using static?
This is wrong.
Try changing {% load staticfiles %} <img src="{% static 'images/right-arrow.svg' %}" /> to
{% load static %} <img src="{% static 'images/right-arrow.svg' %}" /> and you also need to consider which app you should find your static files.

Django: Static vs Assets

I'm new in Django,
I'm adding new CSS files to my application!
<link href="{% static 'css/style.css' %}" rel="stylesheet">
EDIT:
I'm using {% load static %} before rendering the teamplate!
I can't see any change when add files to the /static/ folder
(it works when adding them to /assets/)
My Question:
What the deference between the two folders? Do I need to add the new files to /static/ anyway? Because I see the var {% static .. %}

how to display images?

please help bring images into the template
in django1.6 I uploaded via the form in the database image. screenshot. ( checked that the images loaded in a specific directory ) . then imported into the template file settings.py variable BASE_DIR and to all records of a table . then tried in the template to display the image as follows:
{% for entrie in all_entries_carousel %}
<a href="{{ entrie.link }}" title="{{ entrie.title }}" target="_blank">
<img src="{{ BASE_DIR }}/{{ entrie.image }}" width="300" height="200" alt="{{ entrie.title }}" />
</a>
{% endfor %}
results in images that I have not loaded and displayed.
in the source path
c:\Python33\django_projects\proj1/carousel/media/images/img1.png
please tell me how can I still display the image. Sure , there is a path without importing BASE_DIR
ps this way does not work proj1/carousel/media/images/img1.png
You need to configure your static files.
From Django docs:
Make sure that django.contrib.staticfiles is included in your
INSTALLED_APPS.
In your settings file, define STATIC_URL, for example:
STATIC_URL = '/static/'
In your templates, either hardcode the url like
/static/my_app/myexample.jpg or, preferably, use the static template
tag to build the URL for the given relative path by using the
configured STATICFILES_STORAGE storage (this makes it much easier
when you want to switch to a content delivery network (CDN) for
serving static files).
{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
Store your static files in a folder called static in your app. For
example my_app/static/my_app/myimage.jpg.

Fetch Static files from Server in Django

I'm using a development server for django. I want to grab my static files from a server. htts://www.example.com/static
How do I do this in Django?
Currently I'm trying to to change the STATIC_URL from '/static/' , but it fails whenever I change it. By fail I mean that the html still loads, but the site can't access and load my static files.
this is the url of the static that will be used in template STATIC_URL = '/static/'
add the desire path to the static files dirs var
STATICFILES_DIRS = ('/var/www/my_site/my_path',)
please note that the path doesn't end with backslash
please note that the trailing comma
now in your templates use
<head>
{% load staticfiles %}
<link href="{% static "css/style.css" %}" rel="stylesheet">
</head>
this link will resolve to my_site/static/css/style.css
and will be in the folder /var/www/my_site/my_path/css/style.css
href="my_site/static/css/style.css"
maps to
STATICFILES_DIRS : /var/www/my_site/my_path/css/style.css

Categories