Django admin page is missing images/css/js - python

I have found a few questhons re my problem, you the solutions didn't help me so I started a new question. Basically that's how I see the admin page
Apache's config:
<VirtualHost *.*.*.*:80>
ServerName ********.org
ServerAdmin ****#******.org
WSGIScriptAlias / /var/www/webproxy/webproxy/wsgi.py
DocumentRoot /var/www/cgi-bin/
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
ErrorLog /var/www/webproxy/apache/error.log
LogLevel warn
CustomLog /var/www/webproxy/apache/access.log combined
Alias /media/ "/var/www/webproxy/media/"
<Directory "/var/www/webproxy/media/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
<Directory "/var/www/webproxy/static/">
Order deny,allow
Allow from all
</Directory>
This is what I have in urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
from core import urls as core_urls
from settings import MEDIA_ROOT, WEBPROXY_MEDIA_ROOT
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': MEDIA_ROOT}),
(r'^ui/(?P<path>.*)$', 'django.views.static.serve', {'document_root': WEBPROXY_MEDIA_ROOT}),
)
settings.py:
MEDIA_ROOT = '/var/www/webproxy/media/'
STATIC_ROOT = '/var/www/webproxy/static/'
WEBPROXY_MEDIA_ROOT = '/var/www/webproxy/static/media/'
I'm not sure what's wrong here. Any help would be really appreciated.

First, you shouldn't be serving the static files via django.views.static.serve in production as that is meant to be used only for local development (read docs here):
There may be files other than your project's static assets that, for
convenience, you'd like to have Django serve for you in local
development.
Second, your Django Admin static files reside under the location Django was installed at. I find using directly these files a better solution than copying (or symlink'ing) the Django admin static files under my own static files directories.
Example Django static files location on my server:
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
Therefore in your web server config you have to point the url /media/admin to
[my_django_location]/contrib/admin/static/admin/
If you're doing things locally, then you might need another entry in your urls.py:
(r'^admin/static/(?P<path>.*)$', 'django.views.static.serve', '/path/to/your/admin/files/'),

Related

Django 2.0 Static Files not recognized in Apache2

My website is online, but it can't find the static files ;/
The configurations seem to be all ok, can't find the error...
I don't have admin permission on the server, so it kind'a sucks..
but, at least they pass me the configurations they've put..
Apache Configuration:
<VirtualHost *>
ServerAdmin suporte#pop-rs.rnp.br
DocumentRoot /home/metis/public_html/
ServerName agata.pgie.ufrgs.br
ServerAlias www.agata.pgie.ufrgs.br
#ErrorLog /var/log/apache2/agata-error.log
#CustomLog /var/log/apache2/agata-access.log common
<Directory /home/metis/public_html/static>
Require all granted
</Directory>
<Directory /home/metis/public_html/AGATA/textMiningProject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess agata python-path=/home/metis/public_html/AGATA python-home=/home/metis/public_html/AGATA/agataenv
WSGIProcessGroup agata
WSGIScriptAlias / /home/metis/public_html/AGATA/textMiningProject/wsgi.py
</VirtualHost>
Django Version: 2.0
settings.py
Static files (CSS, JavaScript, Images)
https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_PATH = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/' # You may find this is already defined as such.
STATIC_ROOT = '/home/metis/public_html/static'
STATICFILES_DIRS = [
STATIC_PATH
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
And it was asked to the admins to insert the following, no changes in the result tho..
Alias /media/ /home/metis/public_html/AGATA/media
Alias /static/ /home/metis/public_html/static
<Directory /home/metis/public_html/static>
…
Order allow,deny
Allow from all
</Directory>
can someone help?
Edit 1:
The output for collecstatic:
0 static files copied to '/home/metis/public_html/static', 166
unmodified.
The error on the site:
simple-sidebar.css Failed to load resource: the server responded with
a status of 404 (Not Found)

Django with Apache working for admin page but gives 404 for all other urls

I am a django beginner.
I tried to deploy my django(1.10) site from local server using apache2 (2.4.7)
Port opened.
Admin page is getting opened properly but all other views are not getting opened.
URLS.PY
from django.conf.urls import url,include
from django.contrib import admin
from django.views.generic import RedirectView
from django.conf import settings
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^treedump/',include('treedump.urls')),
url(r'^$', RedirectView.as_view(url='/treedump/', permanent=True)),
url(r'^accounts/', include('django.contrib.auth.urls')),
]
TREEDUMP.URLS
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$',views.index,name='index'),
url(r'printx$',views.printx,name = 'printx'),
url(r'download$',views.download,name = 'download'),
]
.CONF FILE IN APACHE
<VirtualHost *:80>
ServerName *********
DocumentRoot /var/www/html/
Alias /static /data1/ekmmsc/webapp/ekmmsc/static
WSGIDaemonProcess ekmmsc python-path=/data1/ekmmsc/webapp/ekmmsc python- home=/data1/ekmmsc/webapp/ekmmsc/ekmmscenv
WSGIProcessGroup ekmmsc
WSGIScriptAlias /ekmmsc /data1/ekmmsc/webapp/ekmmsc/ekmmsc/wsgi.py
# Other directives here ...
<Directory "/var/www/html/">
allow from all
order allow,deny
AllowOverride All
AddType text/css .css
AddType text/javascript .js
</Directory>
<Directory "/data1/ekmmsc/webapp/ekmmsc/static">
Require all granted
</Directory>
<Directory "/data1/ekmmsc/webapp/ekmmsc/ekmmsc">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
OUTPUT
Not Found
The requested URL /treedump/ was not found on this server.
Any help will be appreciated , i have been working almost for 2 days on this.
Please help.
You should use pattern_name rather than url in your view. This will make Django look up the URL by name, rather than using a hard-coded value, so it will take into account the script prefix.
RedirectView.as_view(pattern_name='index', permanent=True)),

Apache2 host, url not served with mod_wsgi

Just stated to host a website with Django, the problem is like this.
I put an html file for testing under /usr/local/django/virenv/Personal/mysite.com/, which is also the Document root in 000-default.conf, that page can be access via mysite.com
Then I started a django project and an app, in the app/view.py I put
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, a django view.")
In the app/urls.py I put
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^index$', views.index, name = 'index'),
]
added the following to project/urls.py
urlpattrens = [
url(r'^home/', include('app.urls')),
# below is admin url created with startproject
url(r'^admin/', admin.site.urls)),
]
lastly I turn DEBUT=False in project/settings.py, and added ip to ALLOWED_HOSTS
After python manage.py runserver ip:8080 , ip:8080/home/index, and ip:8080/admin are all available.
Next I tried restart apache2, went to mysite.com/admin and mysite.com/home/index
both got
404
Not Found The requested URL /admin was not found on this server.
I guess the mod_wsgi is not working.
The httpd.conf looks like
LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias / /usr/local/django/virenv/Personal/mysite.com/project/wsgi.py process-group=mysite.com
WSGIPythonHome /usr/local/django/virenv
WSGIDaemonProcess mysite.com python-home=/usr/local/django/virenv python-path=/usr/local/django/virenv/Personal/mysite.com
WSGIProcessGroup mysite.com
WSGIApplicationGroup %{GLOBAL}
<Directory /usr/local/django/virenv/Personal/mysite.com/project>
<Files wsgi.py>
Order allow,deny
Allow from all
Require all granted
</Files>
</Directory>
<Directory /usr/local/django/virenv/Personal/mysite.com/>
Order allow,deny
Allow from all
Require all granted
Options -Indexes +FollowSymLinks -Includes
</Directory>
000-default.conf looks like
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /usr/local/django/virenv/Personal/mysite.com
<Directory /usr/local/django/virenv/Personal/mysite.com>
AllowOverride All
Options +FollowSymLinks
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I use ubuntu 16 lts with apache2.4, django 1.10
every directory and file along this path
/usr/local/django/virenv/Personal/mysite.com/*
is set to chmod a+x and chmod a+r
Any guesses or solution?
EDIT: by the way, in wsgi.py
import os
import sys # added from other stack overflow answers
import django.core.handlers.wsgi as WSGIhand # added from other posts
os.environ.["DJANGO_SETTINGS_MODULE"] = "project.settings"
application = WSGIhand.WSGIHandler()

Forbidden access for django apache2 media files

I have trouble with access my files from django admin site. I save files without any trouble, but when I try open it, I get error:
Forbidden
You don't have permission to access /media/file.pdf on this server.
In django project settings:
STATIC_URL = '/static/'
STATIC_ROOT = '/full/path/to/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
In project urls.py:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In my virtualhost I added this settings:
Alias /static/ "/static/folder/"
Alias /media/ "/meida/folder/"
<Directory "/static/folder">
Require all granted
</Directory>
<Directory "/media/folder">
Require all granted
</Directory>
But still get this error. Where can be bug/mistake?
Edit 1: apache error log gives:
client denied by server configuration: /etc/apache2/home
Change /etc/apache2/conf-available/php5-fpm.conf. Replace every
Order Deny,Allow
Deny from all
to
Require all granted
The configuration has to be enabled by a2enconf php5-fpm.
EDIT
Follow the Basic Configuration: https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/
EDIT 2
Do the changes and add this to your virtualhost settings:
<Directory /usr/local/django_apps/myapp/static>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Note that the path is from the root!

Hosting static website with Django

I have Django app running live on AWS at, www.domain.com/admin. It doesn't posses any html pages, we only make use of Django-Admin.
Now I have to host a website at www.domain.com.
I have my website package in this form,
site
|-sass
|-js
|-img
|-fonts
|-css
|-index.html
I copy-pasted my site folder inside my Django app at my_django_app/templates/
Also, added this :
url(r'^$', TemplateView.as_view(template_name='site/index.html')),
inside my_django_app/my_django_app/urls.py.
And, updated my settings.py with,
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'templates/site'),
]
Now when I runserver and go to www.domain.com, it loads my html file without CSS, JS and Images.
Please suggest me what I am doing wrong.
I am a beginner to python and also never hosted a website ever before.
This answer is a reply to this comment, not an answer to the original question.
In your Apache config, change: WSGIScriptAlias / to WSGIScriptAlias /admin.
Add this before the </VirtualHost> closing tag:
Alias / /path/to/static/site/
<Directory /path/to/static/site/>
Options Indexes FollowSymLinks
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
I haven't tested it, but I believe this should work.

Categories

Resources