Django 3 admin link is coming without css - python

The Admin link is missing css, returns 404 when doing view source.
The .conf file is
<VirtualHost *:80>
ServerAdmin webmaster#example.com
DocumentRoot /var/www/django/medicalai
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /var/www/django/medicalai
<Directory /var/www/django/medicalai/static>
Require all granted
</Directory>
<Directory /var/www/django/medicalai>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess medicalai python-path=/var/www/django/medicalai python-home=/var/www/django/venv
WSGIProcessGroup medicalai
WSGIScriptAlias / /var/www/django/medicalai/medicalai/wsgi.py
WSGIPassAuthorization On
</VirtualHost>
The Static file contains.
STATIC_DIR = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
The static folder and files are here.
/var/www/django/medicalai/static/admin/css
All the files and folders are downloaded by using
python manage.py collectstatic.
But there is no CSS when I open http://example.com/admin/login/?next=/admin/
Please help to point the mistake.

This issue could happen because you do not have STATIC_ROOT configured when you run
python manage.py collectstatic
What this setting does is to copy all the static files from all the STATICFILES_DIRS directories into a single ROOT prepared for production serving.
STATIC_DIR = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
STATIC_ROOT = '/var/www/django/medicalai/static/'

Related

Django - Apache2 can't load media files from media folder

I installed an linux server with apache where I want to run my Django app. So I set everything up and when I started the app everything was shown to me except for the pictures.
I couldn't find a error inside my site.conf
<VirtualHost *:80>
ServerName MYHOST
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Alias /robots.txt /var/www/mysite/static/robots.txt
Alias /favicon.ico /var/www/mysite/static/favicon.ico
Alias /static/ /var/www/mysite/static/
Alias /static/ /var/www/mysite/media/
<Directory /var/www/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/mysite/static>
Require all granted
</Directory>
<Directory /var/www/mysite/media>
Require all granted
</Directory>
</VirtualHost>
or inside my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/mysite/static'
MEDIA_ROOT = '/var/www/mysite/media'
MEDIA_URL = '/media/'
What I did was setting debug = True and inside linux console I run python3 manage.py collectstatic after reloading apache the website was shown with all its css and js but for every image request I get an error 404.
Inside my templates/base.html I call {% load static %}. I tried to replace it with {% load staticfiles from static %} but that crashed my entire app (err 500)
I use Django version 3.x and apache2

Permission denied when uploading file (Django, Apache2)

I deployed a beta version of my django app to DigitalOcean and I am serving it using Apache2 for both python and static files.
Here is the issue, when I upload a file, it gives me the following error:
PermissionError at /admin/dashboard/marque/add/
[Errno 13] Permission denied: '/verauto/site/public/media/marques'
Here is an excerpt of my settings.py file, as well as the sites-available configuration for apache2:
settings.py:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
#MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_ROOT = '/verauto/site/public/media'
MEDIA_URL = '/media/'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
#STATICFILES_DIRS = (
# os.path.join(BASE_DIR, 'verautonoapi/static'),
# os.path.join(BASE_DIR, 'boot'),
#)
#STATIC_ROOT = os.path.join(BASE_DIR, 'root')
STATIC_ROOT = '/verauto/site/public/static'
apache2 configuration:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog /verauto/site/logs/error.log
CustomLog /verauto/site/logs/access.log combined
alias /static /verauto/site/public/static
<Directory /verauto/site/public/static>
Require all granted
</Directory>
alias /media /verauto/site/public/media
<Directory /verauto/site/public/media>
Require all granted
</Directory>
<Directory /verauto/app/verauto-staging/verautonoapi/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess verauto python-path=/verauto/app/verauto-staging/ python-home=/verauto/verenv
WSGIProcessGroup verauto
WSGIScriptAlias / /verauto/app/verauto-staging/verautonoapi/wsgi.py
</VirtualHost>
As you can see, in my attempts to solve the issue, I added the media directory in the apache2 configuration, I also did the following commands for chmod rights:
sudo groupadd XXXXXX
sudo adduser www-data XXXXXX
sudo chgrp -R XXXXXX /verauto/site/public/media/
sudo chmod -R 760 /verauto/site/public/media/

Django Static files not found

I am working on a website which uses Django framework. I have put my project related static files in the folder called our_static and collectstatic files in static. Below are my settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "our_static"),
]
base.html file:
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{% static 'style.css' %}" />
</head>
Here our_static files are not at all getting read. My style.css is in our_static folder.
EDIT:
I am using AWS EC2 ubuntu 14.04 as my server, the website is working fine in localhost but not in AWS ubuntu server. I am using the Apache2 server.
More config:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_FINDERS =[
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
my Apache2 config:
Alias /static /home/ubuntu/pythonserver/static
<Directory /home/ubuntu/pythonserver/static>
Require all granted
</Directory>
<Directory /home/ubuntu/pythonserver/pythonserver>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess pythonserver python-path=/home/ubuntu/pythonserver python-home=/home/ubuntu/knowmenow/
WSGIProcessGroup pythonserver
WSGIScriptAlias / /home/ubuntu/pythonserver/pythonserver/wsgi.py
Your issue is in your Apache configuration - probably in your httpd.conf file. Take a look at any tutorial like https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-centos-7#configure-apache which will explain how to set this up. An example httpd.conf file would look like the following if you're using mod_wsgi:
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject:/home/user/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py
You should also inspect the output of your Apache error logs, which will help debug. They're usually at /var/log/apache2/error.log

404 Error while serving Django 1.7 static files on Apache

I've been a django developer for years now, yet a small practice project with 1.7 is giving me a great headache when it comes to serving static files.
I've set STATIC_ROOT
STATIC_ROOT = '/var/www/mydomain/static'
I've set STATIC_URL
STATIC_URL = '/static/' #as default
I'm not using STATICFILES_DIRS since I have one app called 'pages' and it's in INSTALLED_APPS. On localhost the static files are served correctly
I'm using Ubuntu 14.4 and Apache/2.4.7
My apache conf is
<VirtualHost *:80>
ServerName mydomain.net #I own the domain and pointed it correctly in GoDaddy
ServerAlias www.mydomain.net
Alias /static/ /var/www/mydomain/static
<Directory /var/www/mydomain/static>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/mydomain/mydomain/wsgi.py
<Directory /var/www/mydomain/mydomain>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mydomain.net python-path=/var/www/mydomain:/var/www/mydomain/venv/lib/python2.7/site-packages
WSGIProcessGroup mydomain.net
</VirtualHost>
I've run collectstatic and confirmed all static files are in /var/www/mydomain/static/*.
The site loads, but I get a 404 on all css and js files.
All debugging efforts have failed. I've removed the STATIC_ROOT dir to expect a 403, but still getting a 404. I've chown'd all files/folder to root for testing; nothing. I created a deploy user and chown'd all files/folders to it; nothing. I've chown'd all files/folders to www-data; nothing!!
Is there a new config in Apache 2.4+ that's throwing me off?
What you can do is check your /var/www/mydomain path and look for the folder. I think its not there yet. If not then copy your folder to this place so that apache can serve it.
That will be worth looking

Deploy django with apache2

(I am in way over my head) I am trying to deploy my django server on apache2. I have already buildt a quite large front-end application (that is currently deployed with apache2) and do not want to serve any views through django, rather I want to follow the backend as service principal.
I have followed the instructions here:
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi
After the modwsgi part I get this in my apache log file:
[Tue May 20 12:19:44 2014] [notice] Apache/2.2.22 (Debian) PHP/5.4.4-14+deb7u8 mod_wsgi/3.4 Python/2.7.3 configured -- resuming normal operations
After appending this to the apache config file:
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
WSGIScriptAlias / /var/www/PhotodiceServer/PhotodiceServer/wsgi.py
WSGIPythonPath /var/www/PhotodiceServer
<Directory /var/www/PhotodiceServer/PhotodiceServer>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
This is the content of the wsgi file:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PhotodiceServer.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This is the content of the urls.py file:
from django.conf.urls import patterns, include, url
from django.contrib.auth.models import User, Group
from rest_framework import viewsets, routers
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browseable API.
url(r'^admin/', include(admin.site.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
model = User
class GroupViewSet(viewsets.ModelViewSet):
model = Group
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)
This is the installed apps:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'photod',
)
previously when i entered the ip of the webserver (a raspberry pi) i was automagically routed to index.html that is stored in var/www/.
Now when i enter the ip of the webserve i get 404 page not found, do i have to explicetly say what url will load a static file somehow? and how will this play with the routing in angular? (I have used the angular-ui-router.js)?
(if i set debug =False i get Bad Request (400) instead)
I think you are missing a couple of things in your Apache config:
AddHandler wsgi-script .py
In <Directory xxx>
Options +ExecCGI
SetHandler wsgi-script
Try these, and see what it says then.
EDIT*** as your clearly not familiar with apache!!
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
AddHandler wsgi-script .py
WSGIScriptAlias / /var/www/PhotodiceServer/PhotodiceServer/wsgi.py
WSGIPythonPath /var/www/PhotodiceServer
<Directory /var/www/PhotodiceServer/PhotodiceServer>
Options +ExecCGI
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Another EDIT***
Here is the config I used, (although in the end I just ran uWSGI server and ProxyPass to it as it was a bit of a pain!!)
<VirtualHost *:80>
ServerName my.local.domain
SetEnv APPLICATION_ENV development
Alias /favicon.ico /path/to/project/favicon.ico
AliasMatch ^/static/(.*)$ /path/to/project/static/$1
AddHandler wsgi-script .py
WSGIScriptAlias / /path/to/project/project/wsgi.py
WSGIDaemonProcess _WSGI user=chazmead group=www-data processes=1 threads=5 python-path=/path/to/project/venv/lib/python2.7/site-packages:/path/to/project
WSGIProcessGroup _WSGI
<Directory "/path/to/project/project/">
SetHandler wsgi-script
Options Indexes FollowSymLinks ExecCGI
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>

Categories

Resources