Linking stylesheet to Django website on Apache - django

I'm trying to get my Django website to use a stylesheet that until now worked just fine on a development server. As you can see below, I've been using the static files loader until now but no luck ever since I deployed it on Apache.
I'm hardcoding it now but that doesn't work either. I used collectstatic thus I should have all static files I need. I modified Apache config file to serve static files (I followed tutorial).
<!-- {% load static %}
<link href='{% static "stylesheet.css" %}' rel='stylesheet' type='text/css'>-->
<link href='var/www/html/site/static/stylesheet.css' rel='stylesheet' type='text/css'>
Any ideas?
UPDATE: Config files and settings.py file:
<VirtualHost *:80>
WSGIScriptAlias / /home/pi/site.wsgi
ServerName 192.168.1.159
Alias /static /var/www/html/site/static/
Alias /static /var/www/html/site/media/txt/
Alias /static /var/www/html/site/media/photos/
<Directory /var/www/html/site/media/txt>
Require all granted
</Directory>
<Directory /var/www/html/site/media/photos>
Require all granted
</Directory>
<Directory /var/www/html/site/static>
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/html/site/>
Allow from all
</Directory>
</VirtualHost>
Settings.py:
STATIC_ROOT = 'static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
'/var/www/html/site/static/',
]
And in Apache.conf file I just added this to the end:
WSGIPythonPath /var/www/html/site
Changes made:
STATIC_ROOT = 'static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
'/var/www/html/site/app/static', # this line modified to be more
] # clear
FILES_URL = '/media/txt/'
FILES_ROOT = os.path.join(BASE_DIR, 'media/txt')
MEDIA_URL = '/media/photos/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/photos')
Changes made in site's config file on Apache:
ServerName 192.168.1.159
Alias /static /var/www/html/site/app/static/
Alias /files /var/www/html/site/media/txt/
Alias /media /var/www/html/site/media/photos/
And I'm using this loader to fetch it:
{% load static %}
<link href='{% static "stylesheet.css" %}' rel='stylesheet'
type='text/css'>
When I type the path into terminal and hit Enter the stylesheet opens as expected.

It looks like your Apache configuration isn't quite right. You have three aliases for /static here:
Alias /static /var/www/html/site/static/
Alias /static /var/www/html/site/media/txt/
Alias /static /var/www/html/site/media/photos/
Each of these should point to a different alias, something like this:
Alias /static /var/www/html/site/static/
Alias /media /var/www/html/site/media/
I'm not 100% sure, but I'm guessing that /static is pointing to the last one you declare, /var/www/html/site/media/photos/. You can verify this by checking a file that exists under /var/www/html/site/media/photos/ in the browser like this: http://yoursite/static/photo_that_exists.jpg
See here for more details: https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/modwsgi/#serving-files
Either way, you should only have one Alias definition per path. Give that a try.

As turned out ... All I needed to do is change ServerName 192.168.1.159 to ServerName 127.0.0.1 and static files loaded up like nothing.
Apparently Django in deployment needs to be pointed to 127.0.0.1 which is IP representation of 'localhost'. If I use my internal IP address Django loader for static files won't accept it.
I'd like to acknowledge #FlipperPA who pointed out at other bug I had which I would've otherwise missed. Thank you.
I don't think my understanding of this problem is correct so if you've got better explanation, please do share with me. Cheers.

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/

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...

Serving static files on my own server

im setting a django server, but im having problem with my static's file's:
django settings
STATIC_URL = 'http://localproject/static/'
STATIC_ROOT = '/srv/www/project/static/'
MEDIA_ROOT = '/srv/www/project/public/'
MEDIA_URL = '/public/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
apache
Alias /static/ /srv/www/project/static/'
<Document /srv/www/project/static>
Order allow,deny
Allow form all
</Document>
Alias /public/ /srv/www/project/public/'
<Document /srv/www/project/public>
Order allow,deny
Allow form all
</Document>
So, i have the admin without style and the website, for example http://localproject/public is showing a error about * Not FlatPage matches the given query *
yes, im using django.contrib.staticfiles
Any idea?
Thanks
Two things:
First, ADMIN_MEDIA_PREFIX = STATIC_URL+'admin/'. You can actually use that or change it to ADMIN_MEDIA_PREFIX = 'http://localproject/static/admin/'. /static/admin/ is incorrect in your scenario.
Second, Django is still being handed the request, and 'public' is being passed as a slug to the FlatPages view, which is why you're getting that error. See: https://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/#serving-files for the proper Apache configuration for staticfiles.
Im answering maybe that help someone, the problem was that im using apache (/etc/apache2/sites-available/project), but i was wrong with "use another apache config into the project" /srv/www/project/apache/httpd.conf and setting my Alias in this last one config, when the Alias config need to be in (/etc/apache2/sites-available/project)
using:$ sudo nano /etc/apache2/sites-available/project
and the content for project
<VirtualHost *:80>
ServerName project
DocumentRoot /srv/www/project
<Directory /srv/www/project>
Order allow,deny
Allow from all
</Directory>
AliasMatch ^/([^/]*\.css) /srv/www/project/static/css/$1
Alias /public/ /srv/www/project/public/
Alias /static/ /srv/www/project/productos/static/
<Directory /srv/www/project/productos/static>
Order deny,allow
Allow from all
</Directory>
<Directory /srv/www/project/public>
Order deny,allow
Allow from all
</Directory>
WSGIDaemonProcess project processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup project
WSGIScriptAlias / /srv/www/project/apache/django.wsgi
</VirtualHost>
my settings.py:
MEDIA_ROOT = '/srv/www/project/public/'
MEDIA_URL = 'http://project/public/'
STATIC_ROOT = '/srv/www/project/productos/static/'
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = 'http://project/static/admin/'
And now is working :), remember this is my own server