adding "static" to many entries in a DJango html file - django

I need to add the "static" feature to a number of files in my DJango project as mentioned here: howto: static files
It seems to work. But, the instruction says that one needs to make this change
OLD:
<img src="my_app/example.jpg" alt="My image"/>
NEW:
{% load static %}
<img src="{% static 'my_app/example.jpg' %}" alt="My image"/>
to every line that needs it.
I have several hundred lines of already-existing code that is in the old format. How can one change so many lines so at one time so that they will be in the correct format? In "vi", one can use something like:
:1,$ s/static/{% static '/
but that would only take care of the left-hand-side. The right-hand-side would still need: ' %"} . The files being used are sometimes .css, .jpg, .png, and others.
TIA
Here is a sample:
<link href="/static/wforms/assets/global/plugins/bootstrap-daterangepicker/daterangepicker.min.css" rel="stylesheet" type="text/css" />
<link href="/static/wforms/assets/global/plugins/morris/morris.css" rel="stylesheet" type="text/css" />
<link href="/static/wforms/assets/global/plugins/fullcalendar/fullcalendar.min.css" rel="stylesheet" type="text/css" />
<link href="/static/wforms/assets/global/plugins/jqvmap/jqvmap/jqvmap.css" rel="stylesheet" type="text/css" />
[... snip ...]
<div class="mt-avatar">
<img src="/static/wforms/assets/pages/media/users/avatar80_1.jpg" />
</div>
[... snip ...]
Update
#SLePort - thanks for the answer. For my situation, just made a few changes:
The "sed" script creates the result so that - on the right-hand-side - one gets
%} and not %} " (with the extra quote on the end) - see example below
<link href="{% static "wforms/assets/global/plugins/font-awesome/css/font-awesome.min.css" %} rel="stylesheet" type="text/css" />
<link href="{% static "wforms/assets/global/plugins/simple-line-icons/simple-line-icons.min.css" %} rel="stylesheet" type="text/css" />
So, I just ran
:1,$ s/%}/\'%}/g
in the vi editor and all was fine.
Thanks again!

With sed, you can use backreference and the -i flag to edit you file in place:
sed -i 's|="/static/\([^"]*\)"|="{% static "\1" %}|' file

the solution for me is to apply a regex using some text editor. Use replace:
my_app
for:
{% static 'my_app
and for the final bit:
jpg
for:
jpg' %}
If you use linux, gedit or xed. In windows notepad++ is a good choice for these things.(Or your code editor sure has the function replace)
Edit
Komodo edit lets you replace in different files at the same time.
Hope it helped.
Edit 2
Here are the regexp that seem to work:
(href="|src=")
replace with
\1\2{% (adjust this as you need)
The last part:
(jpg|\.css)
replace with
\1\2' %}

Related

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

Static files with bootstraps directory

I thought it would be easier to download bootstrap and adjust it to my code, instead of messing about a cdn. So I downloaded bootstrap and my directory looks like this.
My static directory is properly (I think) setup since my style.css and maps.js are working fine.
In my base.html I reference the files with:
The old way <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel='stylesheet' type='text/css' href="{% static 'style.css' %}" />
<script src="{% static 'maps.js' %}"></script>
<script src="{% static 'bootstrap-3.3.7' %}"></script>
The static boostrap-3.3.7 is not working. The cdn worked however. My question is how would my static import look to have this setup with the bootstrap directory properly work?
I'm sorry if it's a stupid question.
You are trying to include a complete directory into your HTML, which is impossible. You should adjust the path in {% static 'bootstrap-3.3.7' %} to the location of the actual file you need.
<script src="{% static 'bootstrap-3.3.7/dist/js/boostrap.min.js' %}"></script>
Note, don't forget to add the css file too, you need to include them both individually. Which will probably be:
<link rel="stylesheet" type="text/css" href="{% static 'bootstrap-3.3.7/dist/css/boostrap.min.css' %}"/>

flask-bootstrap bootstrap_find_resource

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 %}

replace href and url by the <c:url /> tag

I'm working on a web application using JSP and JSTL in presentation layer.
When I get html from designer, my jsp file is updated as below:
...
<link rel="stylesheet" type="text/css" href="<c:url value="/fo/inc-css/normalize.css"/>" />
<link rel="stylesheet" type="text/css" href="<c:url value="/fo/inc-css/font.css"/>" />
<link rel="stylesheet" type="text/css" href="inc-css/jquery.jscrollpane.css" />
<link rel="stylesheet" type="text/css" href="inc-css/csspie.css" />
...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="inc-js/plugins.js" type="text/javascript"></script>
<script src="inc-js/plugins/jquery.fitvids.js" type="text/javascript"></script>
<script src="inc-js/plugins/jquery.bxslider.min.js" type="text/javascript"></script>
...
<h1>Home Page</h1>
...
<ul>
<li>Log out</li>
<li>My Profile</li>
<li>Help</li>
<li>Contact Us</li>
</ul>
...
There is many href/url without <c:url />(I don't show the whole file because it is very long). I want to replace all href/url with the <c:url /> tag but #, / and full url. For example
href="inc-css/jquery.jscrollpane.css"
should become
href="<c:url value="/fo/inc-css/jquery.jscrollpane.css" />"
Can anyone suggest me some patterns for searching/replacing in Notepad++ or Eclipse?
Notepad++:
You could use this in find:
href="([^"<]{2,}")
And in replace, you can use:
href="<c:url value="/fo/\1 />"
^^
The ^^ indicate where the captured group is being inserted.
EDIT: To match both href and src, you will use this:
(href|src)="([^"<]{2,}")
And replace:
\1="<c:url value="/fo/\2 />"
In Eclipse you could
Find href="([^#][^\"]+)"
Replace with href="<c:url value="$1" />"
Although I think because you have quotes within quotes, you may be better off doing
Replace with href="<c:url value='$1' />"

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