I Build a WebApp and I am stuck on a Problem.
Images are not showing in Production (DEBUG = False)
settings.py
MEDIA_ROOT= '/home/new/gitrepo/main/media'
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = '/home/new/gitrepo/static/'
What have i tried
I tried python manage.py collecstatic BUT images are still not showing in the Website.
I found many answers on this Topic like THIS. BUT it didn't worked for me.
I don't know what i am doing wrong.
Any help would be appreciated.
Thank You in Advance.
Was it working in development? I think yes
open Pythonanywhere.com => choose "Web" from navbar => scroll to static files
and here you let pythonanywhere know where are the URLs for showing static data from your server
Add those two
URL Directory Delete
/static/ /home/myWebsite/static
/media/ /home/myWebsite/media
The /home/myWebsite/media is a path to where you store the static files for the given URL
here is a screenshot of my configurations
If it's not showing make sure you have something in your main URLS.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Related
I understand there are multiple (if not 100s) of questions pertaining to my issue. After trying all, I am here finally to ask. I am able to upload the image and the image path in the model. Example of an image field from model:
<ImageFieldFile: static/image1_FzGpiKx.jpeg>
My static folder is right where the project and app folders are. In similar hierarchy. I have the following settings in my settings.py file:
MEDIA_ROOT = '/static/'
MEDIA_URL = '/'
In my app level urls.py, here is what I have for rendering these images:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
After all this settings , my template shows the following as img src:
/static/image1_FzGpiKx.jpeg
Here is how I render in template:
<img src={{article.image.url}} />
Yet it just renders the typical broken image icon. Can someone help me here? Thank you!
MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/static/'
use this in your settings file, and upload a new image and then check it
I'm trying to run a jango site, static files are working, media is not loaded from the media folder, if the picture in static files is visible. The folder is listed correctly, pycharm gives a fall in the folder
setings.py
STATIC_ROOT = '/home/static/'
STATIC_URL = '/static/'
# MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
MEDIA_ROOT = '/home/media/'
MEDIA_URL = '/media/'
urls.py
if settings.DEBUG:
import debug_toolbar
# Server statics and uploaded media
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
# Allow error pages to be tested
urlpatterns += [
url(r'^403$', handler403),
url(r'^404$', handler404),
url(r'^500$', handler500),
url(r'^__debug__/', include(debug_toolbar.urls)),
]
I use django oscar, this can be the cause of the problem?
To solve this problem, you should executed pycharm with run as administrator or apache service executed run as administartor ,or Enter the absolute path to the media file like:
"C:\Users\dr_r00t3r\DjangoProject\media"
Perhaps the access to the desired folder is not possible. To do this, you must run the following command:
chown -R $user:www-data /home/media/
or
chmod 755 -r /home/media
Yes, if I use full path to media, it's working.
like this:
MEDIA_ROOT = 'F:/Work/Mebli1/backup-7.30.2018_21-56-35_u6487/homedir/furniture/furniture/home/media'
Like the 1 billionth static file question about Django 1.3. I've been searching and trying so many things but nothing seems to work for me. Any help would be appreciated. Will try and give as much information as possible.
URL FILE
urlpatterns = patterns('',
url(r'^projectm/statictest/$','project_management.views.statictest'),)
VIEW
def statictest(request):
return render_to_response('statictest.html',locals())
TEMPLATE
<html><head><title>Static Load Test Page</title></head>
<body>
{% load static %}
<img src="{{ STATIC_URL }}testimage.jpg" />
</body></html>
SETTINGS
STATIC_ROOT = '/home/baz/framework/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = ('',)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_DIRS = (
"/home/baz/framework/mysite/templates"
FILES
-bash-3.2$ pwd
/home/baz/framework/mysite/templates
statictest.html
-bash-3.2$ pwd
/home/baz/framework/mysite/project_management/static
-bash-3.2$ ls
testimage.jpg
Not too sure if there is any other information that would be helpful. But basically when I go to this test page and check the page source, the url is pointing to
<img src="/static/testimage.jpg" />
However the image does not load. I have tried this on multiple browsers too. Maybe I am missing an imort statement somewhere?
cheers for the help
Are you using the built in runserver command or do you serve the Django app some other way?
If you use runserver then your problem is that you don't tell Django where to find your static assets in the filesystem. You need to set STATIC_ROOT to a filesystem path where your static assets can be found. Try setting it to: /home/baz/framework/mysite/project_management/static/
If you are using a different server (like gunicorn behind Nginx for example) then it is the responsibility of the front-end server to intercept requests for /static/ and serve them for you.
Also remember to run the ‘collectstatic’ management command once you have set ‘STATIC_ROOT’.
https://docs.djangoproject.com/en/1.4/howto/static-files/#deploying-static-files-in-a-nutshell
I am having some problems with serving user uploaded files from my Django application:
from models.py:
class Picture (models.Model):
title = models.CharField(max_length=48)
date_added = models.DateTimeField(auto_now=True)
content = models.ImageField(upload_to='pictures')
From the Django admin the files get uploaded to the user_res/pictures/ folder.
from the project's settings.py:
MEDIA_ROOT = 'user_res'
MEDIA_URL = '/user_res/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
Every time I try to reference a static resource (namely css or js files), everything works fine using URLs such as
http://localhost:8000/static/<subfolder>/main.css.
However, I cannot access user uploaded files (which get created by the admin interface in the user_res/pictures folder with a relative URL such as
user_res/pictures/test.jpg
the URL is dynamically created with this line of code from a Django Picture model callable:
return '<img src="{}"/>'.format(self.content.url)
I have no dedicated url-s for either static or media files in the url.py file.
Does anybody have any idea as to how to make Django serve the media files? I understand that for live environments I will need to configure an http server to serve that particular directory, but for now I want to maintain a lightweight development suite.
Thank you.
Edit your urls.py file as shown below.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
edit your projects settings.py to look like:
#Rest of the settings
MEDIA_URL = '/media/'
MEDIA_ROOT = 'media'
STATIC_ROOT = ''
STATIC_URL = '/static/'
Please read the official Django documentation about serving files uploaded by a user carefully. Link to docs: https://docs.djangoproject.com/en/1.5/howto/static-files/#serving-files-uploaded-by-a-user
I think the url attribute returns a relative URL ( Django's FileField documentation ), so you should have:
return '<img src="{}"/>'.format(MEDIA_URL + self.content.url)
Relative URLs won't work, as a user visiting "http://localhost/books/" would be requesting "http://localhost/books/user_res/pictures/test.jpg".
This should be super simple, but somehow its has had me stuck all morning. I'm developing locally, using the django debug server, and with this filestructure:
/project/ (django project)
/static/ (static files)
In the settings.py MEDIA_ROOT and MEDIA_URL are both set to '/static/' and I'm using this in my urls.py
url(r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root': '../static'}),
In my templates, the files that need to be server from the static directory are configured as such:
<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css">
That all works as it should - the javascript/css/images are all served properly from the hompage. However, when I go to a subdirectory, such as http://127.0.0.1:8000/news/ then all the links are broken.
I've tried using a variety of the os.import options to get it to do the relative links properly, but havent had any luck. Is there a way that I could force it to be relative to the base url, or perhaps hardcode it to my filesystem?
Any help would be amazing!
In this line in your urls.py file, the '../static' should be changed to an absolute directory. Try changing it and see what happens.
Your file:
url(r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root': '../static'}),
Should look more like:
url(r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/full/path/to/static'}),
To give you an example, mine is set up a little differently, but I still use a full path.
Here's how mine is setup:
in settings.py
STATIC_DOC_ROOT = '/Users/kylewpppd/Projects/Django/kl2011/assets/'
and in urls.py:
(r'^assets/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_DOC_ROOT, 'show_indexes':True}),
I'm serving my static files from 'localhost:8000/assets/'.
I just had the same problem, and I solved it by removing first slash from STATIC_URL='/static/' and making it STATIC_URL = 'static/' just like philipbe wrote above.
Wierd thing is that '/media/' works fine for MEDIA_URL at the same time (while 'media/' breaks layout).
Anyway - just change STATIC_URL to be equal to 'static/' with no leading slash, and you'll solve it.
How do your links break when you go to a subdirectory? Can you explain that further please.
Does django 1.3 support some kind of strange relative url routing for static media?
If it can be served from the homepage but not others, doesn't that sound exactly like your STATIC_URL is a relative location?
What is your STATIC_URL? It should be absolute and start with a slash.