flask-bootstrap bootstrap_find_resource - flask

looking to specify the themed css for bootstrap. can't get bootstrap_find_resource to look to my locally placed css files. Seems to continue to pull from the /site-packages/flask_bootstrap location.
the current default for bootstrap css is pointing here
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
and this is the macro generating the url
<link href="{{bootstrap_find_resource('css/bootstrap.css', cdn='bootstrap')}}" rel="stylesheet">
bootstrap_find_resource code pointed me to the BOOTSTRAP_SERVE_LOCAL config. setting it to true gave me a local endpoint. which created the local endpoint
<link href="/static/bootstrap/css/bootstrap.min.css?bootstrap=3.3.5.7" rel="stylesheet">
so I placed the bootstrap css in this local app static folder /static/bootstrap/css/. and i verified that flask's app.static_folder is actually mapped to where I put the new files. still pulling from site packages and ignores my local static files.

bootstrap_find_resource is for loading static files distributed with Flask-Bootstrap. For your own files, use url_for.
{% block styles %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='locally/placed.css') }}">
{% endblock %}

Related

Django static CSS file not linking to html document

I am trying to link a css file to my html inside a django project but it does not seem to be linking.
base.html
<head>
<title>Document</title>
{% load static %}
<link rel="stylesheet" href="{% static 'myApp/styles.css' %}">
</head>
my folder structure
You static file(styles.css) should be in a static directory created in the root level.

Python Flask not loading CSS despite following other tutorials?

I looked at some other questions here and followed them but my app is still not loading CSS and I do not know what is wrong. My directory is as follows.
(index.html and edit.html here)/static/css (style.css and animate.css here)
mainfolder/templates
my HTML code to load the CSS is
<link rel="stylesheet"
href="{{ url_for('static', filename='css/style.css') }}">
and
<link
rel="stylesheet"
href="{{ url_for('static', filename='css/animate.css') }}">
In Flask, static files such as JavaScript files or CSS files are served from the static folder in your package or next to your module and it will be available at /static in the application.
A special endpoint static is used to generate URLs for static files. For example:
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename = 'style.css') }}">
<script type = "text/javascript" src = "{{ url_for('static', filename = 'bootstrap.js') }}" ></script>
You need to enter the type of static file when providing the URL of this files. For example:
For CSS: type="text/css"
For JavaScript: type = "text/javascript"
If it still doesn't load, try hard reloading the page. The shortcut keys for different browsers for hard reloading the browser can be found on this page. The shortcut in Chrome (for Windows) to hard-reload is Ctrl+Shift+R

Django Compressor : cache messing up

I have a webapp using django-compressor for js, css and less minification.
I'm using COMPRESS_OFFLINE = True because I'm using django compressor to build a less file containing import to other files (otherwise django compressor does not rebuild files if make change in core.less):
// base.html
{% compress css %}
<link rel="stylesheet" type="text/less" media="all" href="{% static 'less/main.less' %}" />
{% endcompress %}
// main.less
#import "core.less";
#import "variables.less";
#import "utils.less";
#import "sidebar.less";
I am running into the following issue: i have the following .css files in assets/CACHE/css:
2601cbccb2ae.css
52a7aa59f552.css
729b9866970c.css
They are all corresponding to a modification of my core.less file. The issue is that when I log in my webapp, it seems Django-Compress {%compress%} use all those file and not only the last one. So sometimes I have the good design, and if I refresh I got the old one...
// First time the page is ok:
<link href="/static/CACHE/css/2601cbccb2ae.css" media="all" rel="stylesheet" type="text/css"/>
// After reloading I got the old design
<link href="/static/CACHE/css/52a7aa59f552.css" media="all" rel="stylesheet" type="text/css"/>
Each time I refresh the file changes... So I assume there is something related to django compressor cache but I have really no idea how to solve this...
If you guys have an idea, feel free to help me.

including css in Django

I'm new to Django, and i'm having hard time including css styles in a template.
I read this and tried to do the same but it's not working for me.
my Template:
{% load static %}<html><head><link href="{% get_static_prefix %}/style.css" rel='stylesheet' type='text/css' /></head><body>
the HTML i get:
<head><link href="C:/Users/Nayish/workspace/am/src/am/static/style.css"rel='stylesheet'type='text/css' /></head>
Note that this is the folder containing my css.
Thanks, Boris.
Make sure you haven't mixed up the STATIC_ROOT and STATIC_URL settings.
STATIC_ROOT defines where the files are on the storage system (usually your local hard disc for local development), while STATIC_URL defines the URL from where the server serves them. The second one is usually referred to in templates, and it is also the value that the {% get_static_prefix %} template tag returns.
I am guessing you aren't using static css sheets. I always just do:
<html>
<head>
{%block stylesheet %}
<style type="text/css" title="currentStyle">
#import "{{MEDIA_URL}}css/style.css";
</style>
{% endblock stylesheet%}
....
I then set my Media root, and store the files as
MEDIA_ROOT=<fullyquallified patyh>/Media/css/<css files>
MEDIA_URL=http://localhost/mysite/
It should be noted that STATIC_URL defaults to MEDIA_URL if its not defined.

css pulling from the wrong location with {{ STATIC_URL }} on my form page only

I'm using django 1.3
I'm using a base.html template which my pages inherit from. Everything works fine except for one page (which has a form on it).
That page is trying to call the CSS from /links/addlink/css/site.css but this is the wrong location. It is in /static/css/site.css
In my base.html I have <link rel="stylesheet" href="{{ STATIC_URL }}css/site.css" type="text/css" charset="utf-8" />
If I create a completely seperate template and use the full path like <link rel="stylesheet" href="http://127.0.0.1:8000/static/css/site.css" type="text/css" charset="utf-8" /> then my page renders correctly.
What is wrong?
You are not using RequestContextin your view.
See here: Referring to static files in templates