Django in Linux set image

23 views
Skip to first unread message

pujiarahman

unread,
Dec 9, 2018, 9:28:19 PM12/9/18
to Django users
Hi all, 

i have instaled django in ubuntu, 
i have image and i put in folder (img), the locate is 

blog 
urls.py
views.py
admin.py
--------------static
---------------------- blog
-----------------------------dist
-----------------------------------img

When i load my browser The image is fine no problem, the problem is when, 
i config the settings.py  to :

DEBUG = False
ALLOWED_HOSTS = ['localhost', '27.0.0.1', 'xxx.xxx.xxx.xxx', '[::1]']

the image can not found, but when i set Debug to True , the image come back normal.
is there any miss in my config settings.py

this my home.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<li class="user-header">
<img src="{% static 'blog/dist/img/noc.jpg' %}" class="img-circle" alt="User Image">

<p>
NOC
<small>Member since Nov. 2018</small>
</p>
</li>
..............


Regards

Sidik

PASCUAL Eric

unread,
Dec 10, 2018, 3:14:42 AM12/10/18
to Django users

Hi,


Which server are you using ? Is is runserver ?


When running in debug mode, it serves statics, but not when debug mode is turned off.

If you Google with "django runserver static debug" you'll get this hit in the very first ones :
https://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail


The thread explains all the details and gives pointers to the reference documentation.


BTW, never use runserver in production. It is not done for that at all, for performances and security reasons.


Additionally, as explained in Django doc, statics should not be served by the Django app, but by the reverse proxy such as Nginx, which sits in front of the Django app and routes requests to the WSGI server, while serving directly static assets such as img, js, css...


An other option for statics if you don't want to dive too much into your reverse proxy settings for configuring the static routing is to use Django middlewares such as whitenoise (http://whitenoise.evans.io/en/stable/), which does a quite good job for static serving optimization. This should suffice for sites not too much demanding in terms of traffic.


Best regards


Eric


From: django...@googlegroups.com <django...@googlegroups.com> on behalf of pujiarahman <pujia...@gmail.com>
Sent: Monday, December 10, 2018 3:28:19 AM
To: Django users
Subject: Django in Linux set image
 
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7b30d6a2-c1e0-45fb-8722-3b38ed6fd062%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Riska Kurnianto

unread,
Dec 10, 2018, 7:46:28 AM12/10/18
to django...@googlegroups.com
in staging or production env, You need to specify STATIC_ROOT in settings.py and run "manage.py collectstatic".



pujiarahman

unread,
Dec 12, 2018, 9:10:49 PM12/12/18
to Django users
Hi all, 

i will tray it.. thanks for your attention 
Reply all
Reply to author
Forward
0 new messages