TemplateSyntaxError with static django 2 - python

I have this code:
HTML
{% load static %}
<!--[if lte IE 8]><script src="{% static game_reviews/css/ie/html5shiv.js %}"></script><![endif]-->
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'game_reviews',]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',]
STATIC_URL = '/static/'
i had a folder inside my app called static/game_reviews, and inside have multiple folder (js, css, etc.).
and i getting this error :
TemplateSyntaxError at /game_reviews/
Could not parse the remainder: '/css/ie/html5shiv.js' from 'game_reviews/css/ie/html5shiv.js'
Any help?. Thanks in advance.

by the django static-files, you should send path as parameter:
{% load static %}
<!--[if lte IE 8]><script src="{% static "game_reviews/css/ie/html5shiv.js" %}"></script><![endif]-->
<!-- ^^ ^^-->

Related

django-why bootstrap already loaded but gave no effect on the page?

Hi guys I'm new to django, now I'm facing problem that the already loaded bootstrap css template gave no effect on the page. I have read several post of this problem in this site, but none of it work. I use python 3.6.4 and django 2.0.
Here is my settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"), ]
settings.installed_apps :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rango'
]
my index.html :
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<title>Rango</title>
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="{% static "css/bootstrap.css" %}" />
the command prompt said :
[17/Feb/2018 20:06:14] "GET /static/css/bootstrap.css HTTP/1.1" 200 178152
When I copy paste all the bootastrap css style to the head it work
When I call an image from same static folder, it also can show the picture
<img src="{% static "images/alif.jpg" %}" width="500px" height="800px" alt="foto alif">
My debug set True and i use virtual environment
I also check on chrome developer tools>network>css. It said the css run
My folder structure:
tango
|
static
|
css
|
bootstrap.css
Can anyone help me to solve this?
i've figure out the problem, it was the False mimetype, so i add
import mimetypes
mimetypes.add_type("text/css", ".css", True)
and the problem is solved
Replace the static files loader in your index.html
your code:
{% load staticfiles %}
Replace with:
{% load static %}

Django Static Precompiler not compiling files?

Django static precompiler does not seem to be working with scss files for me. I already checked if i had the compiler installed and my settings for django are are follows
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'static_precompiler',
'cms',
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'static_precompiler.finders.StaticPrecompilerFinder',
)
STATIC_URL = '/static/'
STATIC_ROOT = "static"
and i am calling the same from django template as follows
{% load compile_static %}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Spacemailer</title>
{% block seo %}
{% endblock %}
<link rel="stylesheet" href="{% static 'style/main.scss' | compile %}" type="text/css" media="all" />
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
There are no errors whatsoever. The output is the same scss file with no compilations being made. Can someone point out what am i doing wrong with the same ? or some alternatives that will support compiling scss as well as coffee scripts
By default the compilation should be done at run time serving the template with
compilefilter.
{% static "js/alert.es6"|compile %}
Renders
<script type="application/javascript" src="/static/COMPILED/js/alert.js"></script>
If your using a different storage and STATIC_PRECOMPILER_DISABLE_AUTO_COMPILE is True compile using compilestatic before collectstatic
Verify the Compiler configuration
STATIC_PRECOMPILER_COMPILERS = (
('static_precompiler.compilers.SCSS', {
"executable": "/usr/bin/sass",
"sourcemap_enabled": True,
"compass_enabled": True,
"load_paths": ["/path"],
"precision": 8,
"output_style": "compressed",
}),
)

page not found when serving static files via django

so in settings.py I have
STATIC_URL = '/static/'
and also
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp.apps.MyappConfig'
]
hence as you can see django.contrib.staticfiles is there and moreover Debug = True
and in my app directory I have a file existing in this path
myapp/static/css/myapp.css
However, when I go to http://localhost/myapp/static/css/myapp.css
it ends up returning
Page not found (404)
Request Method: GET
what am I doing wrong and how can I get django to serve static files properly?
Include this in your project/urls.py
from django.conf import settings
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Use the option STATICFILES_DIRS in your settings.
An example:
# settings.py
...
STATICFILES_DIRS = [
"/path/to/your/static/files/folder",
]
...
In your templates, use the static template tag like
# some_page.html
...
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>
...
or hard-code like
# some_page.html
...
<img src="/static/my_app/example.jpg" alt="My image"/>
...

Django Serving Static Files not loading

Setting up a new django application with the following settings:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DEBUG = True
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'kb/media')
STATIC_ROOT = os.path.join(BASE_DIR, 'kb/static/')
Setup a template
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>HomePage</title>
</head>
<body>
<img src="{% static 'img/girl.png' %}">
<img src="/media/boy-512.png">
</body>
</html>
I assume there's an issue with the settings of how it's supposed to get the static files. In this case the directory for the static image girl.png is kb\kb\static\img\girl.png
If I can help you, this is my settings.py file and one example in my html template :
I have a project : MyProject and one application : my_project (inside I have static document > images documents > test.png file)
My settings.py file looks like :
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "my_project/static"),)
And my HTML template file with image looks like :
<img src="{% static 'images/test.png' %}" />
Hopfully to help you
add your project name in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'yourprojectname',]

Django cant load static files

I have a webpage in Django and now I want to add some CSS (twitter bootstrap) to it. This is the first I am trying. I have carefully read the docs and did everything said there for the django development server to work. I am using development server with debug=True and django version 1.6.5. My settings.py looks like this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
My files are under /mysite/static/bootstrap/css folder and in my template.html I have this:
{% load staticfiles %}
<link href="{% static "boostrap/css/bootstrap.css" %}" rel="stylesheet" media="screen">
Unfortunately, nothing happens, I see that the development server says:
"GET /static/boostrap/css/bootstrap.css HTTP/1.1" 500 59
which means it cannot find them. I even tried doing the settings without STATIC_ROOT, doing this:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/static/',
)
but then the development server returns:
"GET /static/boostrap/css/bootstrap.css HTTP/1.1" 404 1682
Any help appreciated.
You did everything right, the issue here is a simple typo.
Your static files are located in bootstrap
But, in your html, your wrote: {% static "boostrap/css/bootstrap.css" %}
There is a T missing.
The code below will work:
{% load staticfiles %}
<link href="{% static "bootstrap/css/bootstrap.css" %}" rel="stylesheet" media="screen">
You have to load the STATIC file into your project app folder.
---> If your project Name is myblog. You will find another folder named myblog into myblog and you have to place your static folder there.
---> In setting.py you have to add:
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'myblog/static'), )
In your production mode if you collect static , static file will be copied in root static.
I find this problem many times and every time problem was solved by add url into STATIC_URL i.e.
STATIC_URL = 'http://localhost:8000/static/'

Categories