Django: ugly admin interface - django

I'm using Django on a Debian VM, django server is loaded through nginx. Everything's working fine 'til now, except the admin interface. In fact, the admin site, doesn't load the "look and feel" of the interface. It seems that Css and images aren't loading at all, any ideas?
Thanks.

Change ADMIN_MEDIA_PREFIX to wherever your media is. If you didn't copy the media, copy it from wherever django is stored, there's a media and admin directory.
Similar question: Django admin has no style

Try adding the media aliases. I had the same problem when setting up an nginx proxy to Apache, and after adding the media aliases I solved the problem.
Here's a sample that I have in my an nginx site configuration file:
location /media/ {
alias /opt/django-env/django_project/media/;
}
location /admin_media/ {
alias /opt/django-env/lib/python2.6/site-packages/django/contrib/admin/media/;
}

You have probably set the wrong ADMIN_MEDIA_PREFIX setting or simply not set the server up to serve anything from that URL. If you have set all of that correctly, make sure you have copied (or linked) the Django admin media to your project.

I haven't setted up any serving static files. All requests are served to Django via FastCgi and my nginx conf looks like this:
server {
listen 192.168.61.130:80; ## listen for ipv4
##listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name 192.168.61.130;
access_log /var/log/nginx/localhost.access.log;
error_log /var/log/nginx/localhost.error.log;
location / {
root /var/www/socratie;
index index.html index.htm;
fastcgi_pass 127.0.0.1:8000;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}

Related

Deploying Django with uwsgi and nginx, uwsgi ok but nginx not working

I am trying to run my site, when i run
uwsgi --socket 0.0.0.0:8080 --protocol=http --chdir /opt/virtualenv/landivarpj/ --wsgi-file /opt/virtualenv/landivarpj/landivarpj/wsgi.py
i can access 192.xxx.xxx.xxx:8080 fine and my test text appears,
but if i try to go trought 192.xxx.xxx.xxx i only get an nginx welcome page,
and if i try to go in trought my domain www.xxxxxxxx.com, then it doesnt work at all.
in my project folder(opt/virtualenv/landivarpj) i have a uswgi_params file with
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
then in (opt/virtualenv/landivarpj/landivarpj) the wsgi.py is
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "landivarpj.settings")
application = get_wsgi_application()
in etc/nginx/sites-available is have drlandivar.conf
upstream django {
server 192.xxx.xxx.xxx:8080; # for a web port socket (we'll use this first)
}
server {
listen 80;
server_name drlandivar.com www.drlandivar.com;
charset utf-8;
client_max_body_size 75M; # adjust to taste
location /media {
alias /opt/virtualenv/landivarpj/media;
}
location /static {
alias /opt/virtualenv/landivarpj/static;
}
location / {
uwsgi_pass django;
include /opt/virtualenv/landivarpj/uwsgi_params;
}
}
the site-available and site-enable are linked
what have i done wrong why it work on 192.xxx.xxx.xxx:8080
but not trought the domain and nginx
***** new edit as recommended on nginx/availables-site/drlandivar.conf
upstream django {
server 192.254.145.207:8080;
}
server {
listen 80;
server_name drlandivar.com www.drlandivar.com;
charset utf-8;
client_max_body_size 75M; # adjust to taste
location /media {
alias /opt/virtualenv/landivarpj/media;
}
location /static {
alias /opt/virtualenv/landivarpj/static;
}
location / {
proxy_pass http://django;
}
}
it still give me same problem page only load trought 192.xxx.xxx.xxx:8080 not trought drlandivar.com
please help
You need to proxy_pass instead. The following configuration should do the trick.
upstream django {
server 192.254.145.207:8080;
}
server {
...
location / {
proxy_pass http://django;
}
}
Comparing yours to a working example of mine, one thing stands out. Try
...
location / {
include uwsgi_params;
proxy_pass http://django;
}
Adding --logto /var/log/uwsgi/uwsgi.log to your uwsgi invocation (after creating /var/log/uswgi and making it writeable to the uwsgi process) might shed some additional light.

Django + uWSGI + nginx url mapping

I want to run Django with uWSGI behind a NGINX.
I will use Django as a API service which should live on this link:
project.test/api
The Django project itself is blank (1.9.6), just created a app, migrated and created a superuser.
My Project structure looks like this:
/vagrant/api/here-lives-whole-django
/vagrant/api/uwsgi_params
/vagrant/frontend/some-frontend-work-seperated-from-django
My NGINX setup looks like this:
upstream testing {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name www.testing.test;
charset utf-8;
client_max_body_size 75M;
access_log /var/log/nginx/www.testing.test.access.log;
error_log /var/log/nginx/www.testing.test.error.log;
location /api/ {
include /vagrant/api/uwsgi_params;
uwsgi_pass testing;
}
location / {
root /vagrant/frontend;
}
}
The uwsgi_params file:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
My Django url pattern:
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
Atm. Im running Django with following command:
uwsgi --socket :8000 --wsgi-file wsgi.py
Im able to reach Django but when I try to access
www.testing.test/api/admin
I get a 404 (debug from Django).
Page not found (404)
Request Method: GET
Request URL: http://www.testing.test/api/admin
Using the URLconf defined in testing.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, api/admin, didn't match any of these.
I know it will be something very simple for people who are used to this stuff, forgive me because Im new.
Ps. I found some similar questions where people worked with
uwsgi_param SCRIPT_NAME /api;
uwsgi_modifier1 30;
But this just made my Django tell me a 404 with
Request URL: http://www.testing.test/api/api/admin
when I requested
Request URL: http://www.testing.test/api/admin
I had trouble to understand your question but basically it resumes to this: I want nginx to pass all calls to /api/ to django but django is receiving the URL with /api/ prefixed.
nginx trick (does not work for internal URLs)
I'd use nginx's rewrite to solve this
location /api/ {
rewrite ^/api/(.*) /$1 break;
include /vagrant/api/uwsgi_params;
uwsgi_pass testing;
}
It shall drop the /api/ part of the URL and remain in the block to uwsgi_pass into Django.
django solution (dirty in my opinion)
The above works for the first URL going into django. Yet, django is clueless about the prefix that was removed and will have trouble to generate URLs to itself. That might not be a problem in some simple APIs but it would be a problem more often than not.
I guess, the only viable way is to add the prefix to the base URLconf.
from django.conf.urls import include
mypatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns = [
url(r'^api/', include(mypatterns)),
]
Although, I feel this is a very dirty solution.

django nginx setup issues

In the following i am trying to configure from http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
The following is my conf file below placed in my project directory and have linked the same in the sites-enabled of the nginx directory.
Now when i access my http://127.0.0.1:8000/app1/files?path=/ i end up with 502 Bad Gateway and the nginx error log says
2015/09/21 14:07:41 [error] 8023#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8001", host: "127.0.0.1:8000"
but i am able to access this link http://127.0.0.1:8000/media/a.jpg what am i doing wrong here and how can access my django application
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 127.0.0.1; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/rajeev/django-test/mysite/media; # your Django project's media files - amend as required
}
location /static {
#alias /path/to/your/mysite/static; # your Django project's static files - amend as required
alias /home/rajeev/django-test/mysite/static/; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/rajeev/django-test/mysite/conf/uwsgi_params; # the uwsgi_params file you installed
}
}
Edit1: uwsgi_params file
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
You've missed one point in this tutorial. Configuration of uWSGI for nginx is different than configuration for checking in browser if uWSGI is working not only by used port, but also by protocol.
When deploying uWSGI for nginx, --http should be replaced by --socket. By that change, nginx will understand properly output from your uWSGI server, but your browser won't.
Configuration for browser (to check if uWSGI is working) can work for nginx also, but you must use proxy_pass instead of uwsgi_pass in nginx, and using it is not recommended, uwsgi protocol is faster and better for communication between web server and uWSGI server.

Django + NGINX URL Problem

I have set up Django's FastCGI + NGINX, but it's only working for root url: any request to http://example.com/anything redirects to http://example.com. Django's internal server working fine, NGINX static file serving and FastCGI processing of the root URL is fine, error log is clear. Here is my config's server section:
server {
listen 80;
server_name example.com;
location / {
fastcgi_pass localhost:8000;
include fastcgi_params;
}
location /static/ {
alias /root/web_development/src/web_development/static/;
}
}
What am I doing wrong? Thanks in advance.
Try this configs:
server {
listen 80;
server_name example.com;
location / {
root /home/example.com/foo;
fastcgi_pass 127.0.0.1:8000;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
}
Make sure you've already informed nginx the port which django runs.
You may need to add this line to location /:
fastcgi_split_path_info ^()(.*)$;
from djangoandnginx

Django and Nginx try_files 403 for site root page

I use such Nginx configuration for the domain:
server_name_in_redirect off;
listen 80;
server_name ~^(www\.)?(.+)$;
root /var/www/$2/htdocs;
location / {
try_files $uri $uri/ $uri/index.htm #django;
index index.html index.htm;
}
location #django {
fastcgi_pass 127.0.0.1:8801;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
fastcgi_param REMOTE_ADDR $remote_addr;
}
Django URL config:
urlpatterns = patterns('',
url(r'^$', home, name='home'),
url(r'index.htm', home, name='home'),
url(r'^(?P<name>.*).htm$', plain_page, name="plain_page"),
}
all urls like http://domain.com/somepage.htm works good, except http://domain.com/ it always shows 403 by Nginx.
if you add static index.htm file to the site root - it's opened because of try_files directive
if you have no static index.htm, but call http://domain.com/index.htm page is opened by django
buf it you have no static index.htm and open http://domain.com/ you get no page, but by idea
index.htm should be looked and passed to the django as the last in the try_files chain.
how to make http://domain.com/ work (should call django's index.htm) in this case?
Add this
location = / {
rewrite ^(.*)$ /index.htm last;
}
underneath the root line to do a rewrite of the URI before further processing.
PS. You have probably sorted this out during the year since your asked, but here it is for other to see.
A better solution is you provide a / url in your urls.py is remove the
root /var/www/$2/htdocs;
Then only include the root in location {} blocks where you serve up static assets.