I am new to django. I wanted to upload an image but it doesn't show up on website. Instead, it shows a broken image icon. I tried to use load static block but it still doesn't work.
My html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Website</title>
</head>
<body>
<h1>Hello</h1>
{% load static %}
<img src="{% static 'webdev/static/webdev/images/images.jpg' %}">
</body>
urls.py file:
from django.contrib import admin
from django.urls import path
from homepage import views
urlpatterns = [
path('', views.home_page, name='home'),
path('admin/', admin.site.urls),
]
views.py file:
from django.shortcuts import render
from django.http import HttpResponse
def home_page(request, *args, **kwargs):
return render(request, 'home.html', {})
file tree: https://i.stack.imgur.com/EYxNI.png
The documentation on Serving static files during development explains how to set up static files. You need to add the views that serve the static files to the urlpatterns:
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from homepage import views
urlpatterns = [
path('', views.home_page, name='home'),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
This will serve static files when DEBUG is set to True. In case of production (DEBUG = False), Django does not serve static files. Then you should configure the webserver (nginx/apache/…) to serve the static and media files, or work with a CDN.
Furthermore the path is:
<img src="{% static 'webdev/images/images.jpg' %}">
Related
So I have been trying to figure out why the image will not show and I have watched and read numerous things, but i still can't find anything. This is my first question on here. Thanks for helping out
HTML
{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Nikhil's Portfolio</title>
</head>
<body>
test
<img scr="{% static 'static/homepage/wpap.jpg' %}" alt="why is it not working">
<img scr="{% static 'static/homepage/download.jpg' %}" alt="why is it not working">
</body>
</html>
url.py of my website (not app url)
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('homepage.urls'))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
How my files are arranged
img of file organization
Try this
{% static 'homepage/wpap.jpg' %}
Instead of
{% static 'static/homepage/wpap.jpg' %}
It would need to change from this:
<img scr="{% static 'static/homepage/wpap.jpg' %}" alt="why is it not working">
To this:
<img src="{% static 'static/homepage/wpap.jpg' %}" alt="why is it not working">
The "scr" would change to "src"
I dont know why image is not display on the home page. Please see my code. I am on windows running python3 and Django 2.1. Is there any setting need to be done in files somewhere in Django folders like any config file? Please guide
Settting File:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
URL file:
from django.contrib import admin
from django.urls import path
from django.urls import include, path
import jobs.views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('home', jobs.views.home, name='home'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Model File
from django.db import models
# Create your models here.
class Job(models.Model):
image = models.ImageField(upload_to='images/')
summary = models.CharField(max_length=300)
Home Page File
<html lang="en">
{% load static %}
<head>
And where I want to show the image is end of a div
<img src="{% static '9.jpg' %}" height="200"></img>
</div>
</section>
In settings.py you will need to add settings for MEDIA_ROOT and MEDIA_URL. That is what the ImageFile upload_to relates to. See the documentation here.
You can then for example reference the image in the home page template as follows:
<img src="{{ '/media/images/9.jpg' }}">
I am trying to display user-uploaded images on a webpage. When I upload the image files using the Django admin interface (I made a model for gallery images with a filefield), the image is stored in /media/images correctly. I have my media settings set in settings.py as follows:
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
Project urls.py:
from django.conf.urls import include, url
from django.contrib import admin
import gallery.views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('gallery.urls')),
]+ static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
Gallery urls.py:
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
url(r'^', views.homepage, name = 'home'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Gallery views.py:
def homepage(request):
texts = Sitetext.objects.all()
gal = Galleryimg.objects.all()
gal.order_by('position')
return render(request,'index.html', {"text1":texts[0].text,"text2":texts[1].text,"text3":texts[2].text,"text4":texts[3].text,"title1":texts[0].title,"title2":texts[1].title,"title3":texts[2].title,"title4":texts[3].title,"gallery":gal})
And then this is the part of the template code that accesses the image:
{% for g in gallery %}
<div class="col-md-3 col-sm-4 col-xs-6">
<img class="img-responsive" src="g.imgfile.url" />
</div>
{% endfor %}
When I create a gallery image, a broken image pops up, but the image is not accessed correctly.
I do this and I am able to serve media files from the Django development server:
urlpatterns += [
url(r'^media/(?P<path>.*)$', django.views.static.serve, {
'document_root': settings.MEDIA_ROOT}),]
It should be enough to include that just in the project urls.py.
The issue with yours could be that your media URL entry in urls.py is not correct. You seem to be not matching the actual URL of the media file, which is probably something like /media/something.png. You seem to be just qualifying on /media/. I also think your regex should be ^media/, not /media/.
Remove the static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) part, from your gallery.urls file.
Replace src="g.imgfile.url" with src="{{ g.imgfile.url }}".
Aside question: Why do you pass this big dictionary ({"text1":texts[0].text,...) to your template? It is error prone and bad practice. You should just pass 'texts': texts and inside your template iterate over them with a {% for text in texts %} forloop. So, the render method should be something like render(request, 'index.html', {'texts': texts, 'gallery': gal}).
I have my css located in
static/css/boostrap.css
I currently have 2 views. A login view and a dashboard view.
urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'logins.views.login', name='login'),
url(r'^dashboard', 'dashboards.views.dashboard', name='dashboard'),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
When i load up the login page it looks for the static files in
/static/css/login.css
But when i load the dashboard it looks for it in
dashboad/static/css/bootstrap.css
login.html css reference
<link href="static/css/signin.css" rel="stylesheet">
dashboard.html css reference
<link href="static/css/dashboard.css" rel="stylesheet">
Its adding part of the URL to the path off the static files and can not for the life of me figure out how to stop it.
settings.py
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
'/Users/chrismeek/Documents/Python/virtual/src/static/templates',
)
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = '/Users/chrismeek/Documents/Python/virtual/src/static/static-only'
MEDIA_ROOT = '/Users/chrismeek/Documents/Python/virtual/src/static/media'
STATICFILES_DIRS = (
'/Users/chrismeek/Documents/Python/virtual/src/static/static',
)
You are using relative URLs to link to the assets: so they always start from the current page's directory.
Make sure you always use a leading slash:
<link href="/static/css/dashboard.css" rel="stylesheet">
Even better, use Django's static tag to automatically output the value of STATIC_URL, whatever it happens to be:
{% load static %}
...
<link href="{% static "css/dashboard.css" %}" rel="stylesheet">
I am following the Django 1.6 tutorial on how to work with static files (https://docs.djangoproject.com/en/1.6/howto/static-files/). However, something is not working...
This is what I have so far...
URLS:
#mysite/urls.py:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^myapp/', include('myapp.urls')),
)
#mysite/myapp/urls.py:
from django.conf.urls import patterns, include, url
from myapp import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
VIEWS:
#mysite/myapp/views.py:
from django.shortcuts import render
def index(request):
return render(request, 'myapp/index.html')
TEMPLATES:
#mysite/myapp/templates/myapp/index.html:
{% load staticfiles %}
<img src="{% static "myapp/image1.jpg" %}" alt="Image 1"/>
In the mysite/settings.py file, I have STATIC_URL = '/static/'. In the folder mysite/myapp/static/myapp, I have a file named image1.jpg.
When I run the development server, it runs fine if I just put some other HTML in my index.html file, but when I try to display the image as above, it just shows a little white box where the image should be. Furthermore, if I go to localhost:8000/myapp/static/myapp/image1.jpg, I get a 404 error.
I'm assuming I'm doing something very obviously wrong...but what is it?
One thing - the STATIC_URL in settings.py is relative to the app directory, not the project directory, right?
Thank you!
When you're running in production, your static files are managed by the server (Apache, nginx) however when your DEBUG is set to true in your settings.py you'll have to tell django to serve the static files for you. To do this add the following to your URLs.py:
from django.conf import settings
(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT}),
This will allow static to be searchable and serve your static files.
img src="{% static 'myapp/image1.jpg' %}" alt="Image 1"/>