I just launched my Django server, and trying to access it through browser, but the layout is broken, how to fix this issue?
Thanks all for your quick response, I just fixed this issue with following method:
Environment: Apache and WSGI
Add following line to /etc/apache2/sites-enabled/000-default
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
Add following line to settings.py
STATIC_ROOT = BASE_DIR + '/static'
STATIC_URL = '/static/'
Run python manage.py collectstatic and restart Apache
Related
I'm using Django (Django 4.0.3.) with Apache2 to run a webserver and serving media locally.
Part of the site is a warehouse item tracker and is working fine, user can upload media (images) to items, categories etc and everything is displayed as expected.
The other part displays multiple images downloaded from an ftp server via a script I wrote in python, which downloads images, stores them in its specific folder (in the media folder) and edits the Sqlite3 db to point Django to the correct media path - these images however is not displayed when "debug=False". It works when "debug=True".
When clicking image I get the message:
Not Found
The requested resource was not found on this server.
It's my first Django project so I'm a little out of my depth and I'm not really sure what to google.
Anyone have any ideas how to make this work or how I could do this another way?
My guess would be that it has something to do with static files?
Project structure:
|mysite
|__warehouse
|__static
|__mysite
|__media
| |__lots of folders
| |__(image)
|__cameras
Apache django.config:
Alias /static /home/usr/mysite/static
<Directory /home/usr/mysite/static>
Require all granted
</Directory>
Alias /media /home/usr/mysite/media
<Directory /home/usr/mysite/media>
Require all granted
</Directory>
<Directory /home/usr/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Static/Media root/url:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = 'static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'
Have you tried something like:
STATIC_ROOT = '/home/usr/mysite/static'
STATIC_URL = 'static'
MEDIA_ROOT = '/home/usr/mysite/media'
MEDIA_URL = 'media'
I figured it out.
My main app warehouse have url:
http://127.0.0.1:8000/
My secondary app have url:
http://127.0.0.1:8000/w/
So when I tried to display images it was trying to load from:
http://127.0.0.1:8000/w/media/image.jpg
instead of http://127.0.0.1:8000/media/image.jpg
Added a "/" before the url in html and now it seems to be working fine.
<img src=/{{cam.cam_media}} class="rounded img-fluid" alt="/{{cam.cam_media}}">
Did I learn something from this? I'm not sure yet... but for now I'm just gonna enjoy the feeling of success!
I've been searching in vain for a long time the origin of this issue.
Everything works well on development : csv file upload + deletion but in production, file is well uploaded when saving my model but it's not found when I want to delete it.
views.py (file_name = sources/newone/egr-2022-light_copie.csv)
os.remove(f"media/{str(flow.fl_file_name)}")
settings.py. (custom settings file for prod)
BASE_DIR = Path(__file__).resolve().parent.parent
[...]
MEDIA_ROOT = '/home/django/httpdocs/gsm2/media'
MEDIA_URL = '/media/'
apache conf (+ssl)
[...]
Alias /static /home/django/httpdocs/gsm2/static
<Directory /home/django/httpdocs/gsm2/static>
Require all granted
</Directory>
Alias /media /home/django/httpdocs/gsm2/media
<Directory /home/django/httpdocs/gsm2/media>
Require all granted
</Directory>
<Directory /home/django/httpdocs/gsm2/gsm2>
[...]
I have tried to :
modify the path used in remove() by removing media/... without success.
modify the path used in remove() by adding gsm2/in front of media/ ... uselessly.
to replace my settings.py media path with MEDIA_ROOT = os.path.join(BASE_DIR, 'media')... not better
Changed folder rights to 777 and ownership to 'django' ... in vain
Any idea about the possible origins of my pains ?
Found a workaround using #Naushad solution
Replaced :
os.remove(f"media/{str(flow.fl_file_name)}")
with :
file = os.path.join(os.path.dirname(os.path.dirname(__file__)),
f"media/{str(flow.fl_file_name)}")
os.remove(file)
Still have to analyse this solution for a good understanding.
I am trying to serve Mayan EDMS (a Django project) in a sub URI of another site. already checked and followed instructions from these posts:
1) https://gitlab.com/mayan-edms/mayan-edms/issues/350
2) How to host a Django project in a subpath?
3) https://docs.webfaction.com/software/django/config.html#mounting-a-django-application-on-a-subpath
here is part of my apache config:
# Mayan in running on http://0.0.0.0:8000
ProxyPass /mayan http://127.0.0.1:8000
<Directory "/mayan">
Options FollowSymLinks Indexes
SetHandler uwsgi-handler
</Directory>
Alias "/mayan-static" "/opt/mayan/mayan-edms/media/static/"
<Location "/mayan-static">
SetHandler None
Require all granted
</Location>
I added these in the Django settings file:
USE_X_FORWARDED_HOST = True
FORCE_SCRIPT_NAME = '/mayan'
BASE_PATH = '/mayan'
STATIC_URL = '/mayan-static/'
MEDIA_URL = BASE_PATH + '/media/'
I expected that Mayan will be loaded in http://example.com/mayan,
but it redirects to http://example.com/#/mayan and returns a 404 error.
Did I miss something? or did I do something wrong?
I asked this too in https://gitlab.com/mayan-edms/mayan-edms/-/issues/1002
and https://forum.mayan-edms.com/viewtopic.php?f=7&t=5665.
Good news as apparently it is fixed in commit
https://gitlab.com/mayan-edms/mayan-edms/-/commit/2b567ed24a1e0c431a1d7cedca8e39923d47c5f4
Cheers
I have deployed my application on apache 2.2.21
In my settings.py file i have given like this
MEDIA_ROOT = os.path.dirname(__file__)+'\\media'
MEDIA_URL = '/mymedia/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
i am getting admin style as we run 127.0.0.1:8000/admin(django server)
but unable to get admin style when running on apache
http.conf of apache i have only mentioned as follows
WSGIScriptAlias / C:/Python27/Lib/site-packages/django/bin/myapp/django.wsgi
<Directory C:/Python27/Lib/site-packages/django/bin/myapp>
Order deny,allow
Allow from all
</Directory>
is there any changes or anything else i have missed here please advise me,
thank you,
Try using Apache to serve the static content instead of Django:
Alias /static/ /path/to/static/dir/
I think you just forgot ' in MEDIA_ROOT line.
Ok, then try to add this in your apache conf:
<Location "/mymedia/">
SetHandler None
</Location>
<Location "/static/">
SetHandler None
</Location>
<LocationMatch "\.(jpg|gif|png)$">
SetHandler None
</LocationMatch>
I'm a first time django user. I had to manually add in the following deprecated function to the new django1.4 settings.py file for my admin css to be accessible. How can make my site look pretty without using this deprecated function?
ADMIN_MEDIA_PREFIX = '/static/admin/'
Here are my other settings for your perusal:
STATIC_ROOT = '/home/ubuntu/static/'
STATIC_URL = '/static/'
And my apache http.conf file has:
Alias /static/ /home/ubuntu/static/
<Directory /home/ubuntu/static>
Order deny,allow
Allow from all
</Directory>
You must run
./manage.py collectstatic
to copy all static files from packages to static directory. Of course django.contrib.staticfiles must be in INSTALLED_APPS.
There will be folder "admin" in static folder after that. If it exists and still static files not loaded, then look to your webserver config.