I had static files in django.
Project Structure Sample
I have project structure like above i want to use js and img from asset folder. How can i do this ? How i can avoid using {% static "abc.jpg" %} ?
First you need to keep this files in static folder
Keep these files out of templates like
project_main_folder/static/img/abc.jpg
The recommended way is to use static tag provided by django.
{% static "abc.jpg" %}
Without static tag you can do this like
<img src="host:port/static/img/abc.jpg" />
or
<img src="/static/img/abc.jpg" />
This is not recommend.
Related
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.
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 .. %}
I am new to django. My project folder contain media folder. It contain some images.
How can I display this images in template of another app?
Path : mysite/media/blog/templates/blog/details.html
details.html
{{<img src="media/image_2.jpg">}}
But it does not display anything..
You should set-up the STATIC folder and to configure the media files.
The full reference: https://docs.djangoproject.com/en/dev/howto/static-files/ and also
https://docs.djangoproject.com/en/1.4/howto/static-files/.
However you should use something like this (once you configured properly the STATIC files) :
<img src="{% static "media/myexample.jpg" %}"/>
Edit:
Before using static tag, You must load specific tags incluing on top of template code: {% load staticfiles %} : Referring to static files in templates
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.
My app has a lot of routes that use the same set of static files.
I have to define them for every route like this:
css_reset = url_for("static", filename="reset.css")
css_main = url_for("static", filename="main.css")
css_fonts = url_for("static", filename="fonts.css")
js_jquery = url_for("static", filename="jquery-1.7.2.min.js")
js_main = url_for("static", filename="main.js")
And then, when I render a template it looks like this:
return render_template("person.html",
css_main=css_main,
css_reset=css_reset,
css_fonts=css_fonts,
js_jquery=js_jquery,
js_main=js_main)
I'm new to flask and python and I think that what I'm doing is a bit ridiculous. Can I define them in one place and then just use in my templates, without copying and pasting in every route definition?
Instead of passing these variables to your templates every time you can register them as globals in Jinja:
app.jinja_env.globals.update(
css_reset=url_for("static", filename="reset.css"),
css_main=url_for("static", filename="main.css"),
...
)
Or, better yet, register a helper function:
app.jinja_env.globals['static'] = (
lambda filename: url_for('static', filename=filename))
And then in your templates:
<link ref=stylesheet href="{{ static('main.css') }}">
The simplest way is to use Flask-Assets extension.
from flask.ext.assets import Environment, Bundle
assets = Environment(app)
css_all = Bundle('reset.css','main.css','fonts.css')
assets.register('css_all',css_all)
In template:
{% assets %}
<link rel="stylesheet" href="{{ ASSET_URL }}">
{% endassets %}
You can also compress css and js files for production by using certain options of this extension.
Since you need to use these files in many templates, define them in a base.html template and in every template extend that base.html. You don not have to write them again and again.
You don't need to do that, url_for is for generating urls (so that when you change the structure of a url, you don't need to change it a dozen times). You can just use a fixed path to your static files directly in your templates instead. Just put your static files in /static folder and use it in your template :
<link rel="stylesheet" href="{{ YOUR_SITE_URL_HERE+'/static/main.css' }}">
Instead of replacing YOUR_SITE_URL with your site's url directly, you might want to define a variable in your config.py and use it in your template : {{ config['SITE_URL']+'/static/main.css' }}