Sorl-thumbnail only working with DEBUG=True - django

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>

Related

Linking stylesheet to Django website on Apache

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.

URL not found apache

can somebody assist/point me in the right direction:
have deployed my app on a VPS and can move to various pages of my app , e.g. myIP/django-bookmarks/login or myIP/django-bookmarks/register.....however, when i click on the tabs in the app itself (e.g. register tab), it takes me to myIP/register (without the 'django-bookmarks') and then says "URL not found on this server".
how can i resolve this? note: im new when it comes to deploying and have gotten to this point after a number of errors, tutorial readings and few frustrations this past week.
my Vhost:
VirtualHost *:80>
ServerAdmin admin#example.com
ServerName django-bookmarks
DocumentRoot /var/www/django-bookmarks
# mod_wsgi settings
# WSGIDaemonProcess django-bookmarks python-path= /var/www/MyProject/django-$
WSGIProcessGroup django-bookmarks
WSGIScriptAlias /django-bookmarks /var/www/django-bookmarks/wsgi.py
# Static file alias so static files can be referenced by /static/
Alias /site_media/ /var/www/django-bookmarks/site_media
Alias /robots.txt /var/www/django-bookmarks/robots.txt
Alias /favicon.ico /var/www/django-bookmarks/favicon.ico
AliasMatch ^/([^/]*\.css) /var/www/django-bookmarks/django_bookmarks/site_m$ # Static files permissions
# Used for serving static files.
<Directory /var/www/django-bookmarks/site_media>
Order deny,allow
Allow from all
</Directory>
# Project wsgi permissions
# Used for serving django pages.
<Directory /var/www/django-bookmarks/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files> </Directory>
# Project wsgi permissions
# Used for serving django pages.
<Directory /var/www/django-bookmarks/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
wsgi.py
import os,sys
#comments
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_bookmarks.settings")
sys.path.append('/var/www/django-bookmarks')
sys.path.append('/var/www/django-bookmarks/django_bookmarks')
#comments
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
im pretty sure i made a mistake somewhere, im having a difficult time figuring out where.
any help would be appreciated.
thanks
EDIT:
base.html temp
{% block external %}{% endblock %}
</head>
<body id="home">
<div id="wrapper">
<header>
<img src="/site_media/commune.png" height="200" width="400" /><div styl$
<div class="container">
<nav>
home
popular
{% if user.is_authenticated %}
submit
search
tags
{{ user.username }}logout
{% else %}
login
register
reports
FAQ's
{% endif %}

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/

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/

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