Permissions error when accessing Django admin page served by Apache - django

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/

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!

Sorl-thumbnail only working with DEBUG=True

I'm using Python 3.6.9, Django 3.2.8, sorl-thumbnail 12.7.0 with memcached and apache2
my thumbnails completely disappear and give a 404 when settings.py variable DEBUG is set to False.
I also have to say, that I don't get any errors displayed at all from setting THUMBNAIL_DEBUG = True as I understand it depends on the aforementioned variable which I have to set to False for pictures to disappear, if it does not depend on it I'm not getting any error displayed either.
I'm just loading them inside templates like this:
{% load thumbnail %}
{% thumbnail ann.obj.fotografia_professionista "460x310" as thumb %}<img src="{{ thumb.url }}" data-src="{{ thumb.url }}" class="img-fluid lazy">{% endthumbnail %}
and it works fine as long as DEBUG = True.
I restarted apache2 and memcached, I also used python manage.py thumbnail clear or cleanup but it just doesn't work at all.
I'm delivering the thumbnails at SETTINGS.MEDIA_URL, which is just set to "/media" and that I'm enabling in urls.py like this:
urlpatterns = [
# my paths
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I did read this thread and I set up my urlpatterns the same exact way as it was suggested, but it doesn't solve my problem.
From reading this other thread, I also imagined that the combination between the variable DEBUG and THUMBNAIL_DEBUG could matter, but it seems it doesn't.
I'm supposing memcached is not responsible for this as it works alright with DEBUG = True, which means it's enabled and up and running as far as Django is concerned, so I'm suspecting a bug with either Django or sorl-thumbnail, or maybe apache2 configuration itself.
The problem was in my apache2 site configuration file that wasn't serving the /media Alias as the directory path had changed.
Refer to this page of the documentation in case.
Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
<Directory /path/to/mysite.com/media>
Require all granted
</Directory>
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Serving admin css for django 1.5 using apache

I am moving a application built on Django 1.5 to the development server, running apache, for the first time. I have it mostly running properly, but I am having issues with the CSS serving. I can either get the site's CSS working but not the admin's css or have the admin's css working but not the site's css.
I followed the documentation's and used the collectstatic command to get all the static assets into the STATIC_ROOT folder.
This is my relevant data from my settings file
STATIC_ROOT = '/var/www/projectmanagement/django/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/var/www/projectmanagement/django/projectmanagement/projects/static',
)
This is the relevant data from httpd.conf
Alias /static/admin/ /var/www/projectmanagement/django/static/admin/
Alias /static /var/www/projectmanagement/django/static/
<Directory /var/www/projectmanagement/django/static/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /var/www/projectmanagement/django/projectmanagement/django.wsgi
<Directory /usr/local/wsgi/scripts>
Order allow,deny
Allow from all
</Directory>
and from my vhost.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#pmt.com
ServerName pmt.com
ServerAlias www.pmt.com
DocumentRoot /var/www/projectmanagement/html/
ErrorLog /var/www/projectmanagement/logs/error.log
CustomLog /var/www/projectmanagement/logs/access.log combined
WSGIScriptAlias /projectmanagement /var/www/projectmanagement/django/projectmanagement/django.wsgi
<Directory "/var/www/projectmanagement/html">
php_admin_value open_basedir "/var/www/projectmanagement/html/:/tmp/:/var/www/projectmanagement /django/projectmanagement/"
php_admin_value include_path "/var/www/projectmanagement/html/:/tmp/:/var/www/projectmanagement /django/projectmanagement/"
</Directory>
</VirtualHost>
When I add the line
AliasMatch /([^/]*\.css) /var/www/projectmanagement/django/static/admin/css/$1
to before the first alias in my httpd.conf file, I get the admin css to work but not the site's css. If it's gone then the site's css works but not the admin's. This has me fairly stumped.
Adding that alias will capture all .css requests, but only looks capable of serving the admin css. Removing it means the css requests will reach your WSGIScriptAlias, and I presume you've got DEBUG = True... so django will be serving your static media for you.
I don't know why django's built in static server isn't working for your admin css - have you set up STATICFILES_FINDERS?
Anyway your alias really should be something like this, so it can serve all of your static files:
AliasMatch /static/ /var/www/projectmanagement/django/static/

Django Admin CSS files unaccessible without deprecated ADMIN_MEDIA_PREFIX

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.

CSS files are not appearing in production server

My css files are not working in Production server. Ive deployed using WSGI. Will you please fix my problem? Thank you
css link
<link href="{{ MEDIA_URL}}css/style.css" rel="stylesheet" type="text/css" />
settings.py
CURRENT_PATH = '/home/nibbler/code/project/
MEDIA_ROOT = os.path.join(CURRENT_PATH, 'templates/media')
MEDIA_URL = '/media/'
TEMPLATE_DIRS = (
os.path.join(CURRENT_PATH, 'templates/temp_name'),
)
site-available\default
<VirtualHost *:80>
ServerAdmin me#me.com
ServerName project.org
DocumentRoot "/home/nibbler/code/project/"
ServerName localhost
ErrorLog "/home/nibbler/code/project/logs/apache-error.log"
CustomLog "/home/nibbler/code/project/logs/apache-access.log" common
Options ExecCGI FollowSymLinks MultiViews
AddHandler wsgi-script .wsgi
WSGIDaemonProcess nibbler
WSGIProcessGroup nibbler
Alias /media /home/nibbler/code/project/templates/media/
WSGIScriptAlias / /home/nibbler/code/project/apache/django.wsgi
DirectoryIndex index.html index.cgi
AddHandler cgi-script .cgi .pl
</VirtualHost>
urls.py
if settings.DEBUG:
# static files (images, css, javascript, etc.)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
You have:
Alias /media /home/nibbler/code/project/templates/media/
wrong for a start. Try:
Alias /media/ /home/nibbler/code/project/templates/media/
They must either both have trailing slash or neither have it. You can't have one have it and the other one not have it.
BTW, having:
DocumentRoot "/home/nibbler/code/project/"
is a bad idea. Don't set DocumentRoot to be where your code is. If you were to remove WSGISriptAlias for some reason, all your code would be downloadable by external people.
You are also missing a Directory block with Allow directive in it for where WSGI script files and static files are present. This implies you have outside of this virtual host changed Apache config in some way to say that Apache can serve up files from any directory on your box, which is a bad idea as it strips away one level of security.
You have problems only with CSS files or all the media files?
MEDIA_ROOT - should be full path to your files on the system and it seems you have it, so fine.
MEDIA_URL - try to put the full URL to the media files. Try to reach them manually with the full url and see if the webserver serves them correctly. If yes, then put the full url to the MEDIA_URL.
Last thing you showed with the if settings.DEBUG is not needed in production. You really want to set DEBUG = False on production server.
By the way Django suggest to have two virtual hosts - one for the Django application itself, another one for the media (where you serve just static content directly with the http server, no dynamic stuff there).
Hope that helped a little...