when click on "yo" I am able to see media file but when tried to display image through the image tag it is not seen on webpage
yo
<img src="{{status.adv.url}}" width="250" height="250" alt="advertise"/>
Change this
<img src="{{status.adv.url}}" width="250" height="250" alt="advertise"/>
to
<img src="{% static status.adv.url %}" width="250" height="250" alt="advertise"/>
and include {% load static %} template tag on the top of your template(s)
Your project url.py should have these
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Documentation: Managing static files (e.g. images, JavaScript, CSS)
It was add blocker used by me. so disable your add blocker.
If you want to confirm that it is add blocker open your chrome and in console you will find this "Failed to load resource: net::ERR_BLOCKED_BY_CLIENT"
Related
I am working with images uploaded using ImageFields and upload_topath and name, and these images end up in the correct directory media/img/whatevs/. In my settings.py I have the following two lines.
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Now, in the template that matches my / URL, these images display properly and I can see/inspect that the path ends up pointing correctly to media/img/whatevs/imgname.jpg
However, when I want to display these images in another template called product.html that does not match the / url, then the path ends up pointing to product/media/img/whatevs/imgname.jpg instead of to media/img/whatevs/imgname.jpg, resulting in a 404. Somehow the name of the template is added to the img src path automatically. If I inspect it I see that the text points to media/img/whatevs/imgname.jpg but then if I hover my mouse over it I end up seeing this http://127.0.0.1:8000/product/media/img/whatevs/imgname.jpg.
For clarity I add what I have in my files:
urls.py
urlpatterns = [
path('', HomeView.as_view(), name='home'),
path('product/<slug>', ItemDetailView.as_view(), name='product'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py
class Item(models.Model):
image = models.ImageField(upload_to=get_image_upload_path)
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
product.html, where it does not display
<img src='media/img/whatevs/{{ item.slug }}' class="img-fluid" alt="">
home.html, where it displays properly
<img src='media/img/whatevs/{{ item.slug }}' class="card-img-top" alt="">
I am working with Django 3.0.6 in debug mode.
Any clue what I am doing wrong and why the template name is added to the path and how to prevent it?
The reason why it works on homepage and not in the other pages is because HTML anchors paths are relative since they are missing a slash(/) at the beginning. But still it is best practice to you link Item.image this way:
<img src='{{ item.image.url }}' class="img-fluid" alt="">
if you uploaded images and you configured settings settings.py well and also your urls.py do there is no need of force you html to found the images like this
<img src='media/img/whatevs/{{ item.slug }}' class="img-fluid" alt="">
you have to do it like this
<img src='{{ item.slug.url }}' class="img-fluid" alt="">
this will work better
The problem is you use relative paths in the template, instead of this:
<img src='media/img/whatevs/{{ item.slug }}' class="img-fluid" alt="">
you should have that:
<img src='/media/img/whatevs/{{ item.slug }}' class="img-fluid" alt="">
It would be a good idea to have the model return full path, it's not a good practice to put the paths in the template.
I have checked all the settings for django.
CSS is loaded properly but no styling is visible on web page.
I stopped the server and made changes and start it again, but it didn't work.
The code:
<head>
<title>
fire
</title>
{% load staticfiles %}
<link href="{% static 'css/index2.css' %}"/>
</head>
{% load static %}
Note: better use load static instead of load staticfiles
Must be on the top of the html template
First must configure your static url
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
in settting.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
I tried uploading images from the django admin but the images uploads as an empty image. Here's my settings for my project
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')`
And here's my html for my html handler:
<div class="row featurette">
<div class="col-md-3 ">
<img class="featurette-image img-fluid mx-auto" data-src="holder.js/500x500/auto" src="{crimes.images}" alt="Generic placeholder image" width="200" height="200">
Do u have something like this in your urls.py file? As it is said here.
if DEBUG:
from django.conf.urls.static import static
urlpatterns += static(
MEDIA_URL,
document_root=MEDIA_ROOT)
I have the following structure in the details.html
<p>Files:
{% for file in files %}
<p>{{ file.name }}</p>
{% endfor %}
</p>
This link returns the correct url.
http://localhost/media/file.pdf
However, every time I click, instead of downloading the file, it returns error 404.
What is the correct way to do this operation?
<p><a href="{{ file }}" download>{{ file.name }}</a></p>
if you try to query to on database and print in client side, so in your anchor link write a right path
example:
<p><a href="/media/{{ file }}" download>{{ file.name }}</a></p>
Is this on the local development server or in production?
My guess is you haven't configured Django to serve /media/ files.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
However, do note the admonition in the documentation:
This is not suitable for production use!
I am writing a navigation header in django. And i am having problem navigations between urls that i created.
urls.py
url(r'^home/$', home_page, name="home_url"),
url(r'^about/$', about_page, name="about_url"),
template
<a href="home_url>Home</a>
<a href="About_url>About</a>
When I click on this i am getting something like. mysite.com/home/home_url
How should i configure it so i can navigate to the correct link?
Read Naming urls docs:
For your code:
<a href="{% url 'home_url' %}" >Home</a>
About