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
Related
I am working on Anomaly detection project working with django app. i tried to play video from local folder in django app but stuck. How to play the video?
Add this small piece of code to your template, it should do the work for you.
{% load staticfiles %}
<video width="430" height="340" controls autoplay>
<source src="{% static "video_file_name.mp4" %}" type="video/mp4"> </source> #give your video file name here
</video>
# Make sure you have copied your video file into your django projects static folder
# And specified static path in settings.py
settings.py
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"), )
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.
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.
So I started working on this django project that I took over from someone else. There are several settings files that all inherit from a base.py settings file.
In this base.py settings file there are several static image files defined in there.
For Example:
LOGO_URL="/img/logo.png"
LOGO=STATIC_URL+LOGO_URL
This seems strange to define image locations in this way. Is this best practices? if not what is the best practice?
Thanks
More commonly you would see just the STATIC_URL defined in your settings, and then the logo would be accessed in a template with:
{% load staticfiles %}
<img src="{% static 'img/logo.png' %}" />
The docs give more detailed explanation.
I have an entry in my urls.py:
url(r'^user/(?P<username>\w+)$', views.user_show, name='user_name'),
The problem is, whenever the view is rendered, the static files (CSS, Javascript etc) fail to load because Django prepends the URL match to the requests for the static files. So instead of requesting:
http://localhost:8000/static/myapp/css/main.css
it requests:
http://localhost:8000/user/static/myapp/css/main.css
Is there a way to stop the regex match from affecting the URL requests?
Thanks.
are you using this to load the css file in the template?
{% load staticfiles %}
<link rel="stylesheet" href='{% static "css/main.css" %}'/>