Django Admin CSS files unaccessible without deprecated ADMIN_MEDIA_PREFIX - django

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.

Related

Django/Apache2 not serving media files in production when "manually" added new media

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!

Django : Media file not found when deleting despite it's correctly written when creating (PROD)

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.

How to server favicon.ico with Django and Whitenoise

I use whitenoise for static files and it works fine.
But how can I serve the /favicon.ico file?
There is a setting called WHITENOISE_ROOT, but I don't understand how to use it.
I would like to keep my nginx config simple and serve all files via gunicorn
If you want those files to be managed by collectstatic
Let's assume after running collectstatic, your favicon.ico file ends up being copied in a root subdirectory, located in your STATIC_ROOT directory.
Then, with:
WHITENOISE_ROOT = os.path.join(STATIC_ROOT, 'root')
Whitenoise will serve all files in STATIC_ROOT/root/ at the root of your application.
In your case, STATIC_ROOT/root/favicon.ico will be served at /favicon.ico.
If you don't want those files to be managed by collectstatic
You can have a root_staticfiles folder in your BASE_DIR which simply contains the static files you want to serve at /.
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'root_staticfiles')
In this case, Whitenoise will serve all files in BASE_DIR/root_staticfiles/ at the root of your application.
Update about pathlib (2022-10-04)
Since a while now, the default settings.py Django creates uses pathlib. To be consistent with it, one may replace os.join calls with / operators, eg.:
WHITENOISE_ROOT = STATIC_ROOT / 'root'
You could as per this answer by hanleyhansen add the following line in a base template (used by all further templates):
<link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>
Or you could write a redirect view like this answer by wim with some little modification:
from django.views.generic.base import RedirectView
from django.conf.urls.static import static
re_path(r'^favicon\.ico$', RedirectView.as_view(url=static('favicon.ico'), permanent=True))
I have a django app that uses Whitenoise (hosted on Heroku) and serves my favicon from a separate folder from my static files.
Make a folder root_files at path BASE_DIR/root_files.
In settings.py:
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'root_files')
For a real-life code example checkout Mozilla's Bedrock repo. They have favicons in BASE/root_files and configure WHITENOISE_ROOT in settings.py

Django debug page layout is broken

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

Permissions error when accessing Django admin page served by Apache

I am new to Django development and this site has been invaluable so far and I'm impressed with how much I've learned. That being said, I am running into a problem and I've been crawling through the related posts on this site yet can't seem to find anything that solves my issue.
Here is what I've tried but no luck.
So I am hoping that one of you might be able to take a look at what I'm doing and point me in the right direction. I am trying to get my Apache web server to serve up the CSS files for Django's admin page. But, when visiting: http://localhost/admin/, I get the error: Forbidden. You don't have permission to access /admin/ on this server.
The CSS/JS for my site is located in mysite/static and the CSS/JS for the admin page is located in the Django installation folder, Django-1.5/django/contrib/admin/static/admin. Here are the relevant bits in my settings.py file:
STATIC_ROOT = '/home/me/Desktop/djcode/mysite/production_static'
STATIC_URL = '/static/'
STATIC_DIRS = (
"/home/me/Django-1.5/django/contrib/admin/static/admin",
"/home/me/Desktop/djcode/mysite/static",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
I've run the python manage.py collectstatic command which pulls all the necessary static files from my two directory locations and places them in a folder called production_static in my project folder.
Here is the production_static folder layout:
$ cd production_static
$ ls
admin css images img js
The admin folder contains all the static files needed for the admin page to display. But for some reason, the css folder contains both my site's CSS as well as the admin CSS. Same with the js folder. The images folder is my site's images and the img folder is the admin site's images. I don't know why the collectstatic command is making two copies of the admin's static files.
Here is Apache's httpd.conf file:
AliasMatch ^/([^/]*\.css) /home/me/Desktop/djcode/mysite/production_static/css/$1
AliasMatch ^/([^/]*\.css) /home/me/Desktop/djcode/mysite/production_static/admin/css/$1
Alias /media/ /home/me/Desktop/djcode/mysite/media/
Alias /static/ /home/me/Desktop/djcode/mysite/production_static/
Alias /admin/ /home/me/Desktop/djcode/mysite/production_static/admin/
<Directory /home/me/Desktop/djcode/mysite/production_static>
Order deny,allow
Allow from all
</Directory>
<Directory /home/me/Desktop/djcode/mysite/media>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /home/me/Desktop/djcode/mysite/mysite/wsgi.py
WSGIPythonPath /home/me/Desktop/djcode/mysite
<Directory /home/me/Desktop/djcode/mysite/mysite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I am making sure that I restart the Apache server every time I make changes to the httpd.conf file.
And here is my urls.py file:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'mysite.views.homepage_view'),
# other url-view mappings here
url(r'^admin/', include(admin.site.urls)),
)
My homepage and other pages load fine. The CSS and JS are all wonderful. It's just when I try to access http://localhost/admin/ do I get a permissions error.
Get rid of this alias:
Alias /admin/ /home/me/Desktop/djcode/mysite/production_static/admin/