2

This question is similar to the questions asked here and here. However, solutions to both of them seem to be outdated with the current Django version (1.8).

My django project is running perfectly fine on the developnment server. Now I am trying to deploy it to production and the only trouble is that the css files being references in the default django admin view are not being referenced properly.

Here is what the default admin view is trying to locate it by :

http://localhost/static/admin/css/base.css I am trying to follow the recommended approach given in django docs

We strongly recommend using django.contrib.staticfiles to handle the admin files (along with a Web server as outlined in the previous section; this means using the collectstatic management command to collect the static files in STATIC_ROOT, and then configuring your Web server to serve STATIC_ROOT at STATIC_URL).

Here is the snippet from my 000-default.conf file

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/testapp/ 

Alias /static/ /var/www/html/testapp/static
<Directory /var/www/html/testapp/static>
Require all granted
</Directory>

WSGIDaemonProcess sampleapp python-path=/var/www/html/testapp:/var/www /html/testapp/env/lib/python2.7/site-packages    

WSGIProcessGroup sampleapp

and my STATIC_ROOT and STATIC_URL are configured as below:

STATIC_ROOT = '/var/www/html/testapp/static/'

STATIC_URL = '/static/'

I have collected the static files of django admin using the collectstatic command, still the files are not being served.

I am unable to figure out what is wrong with the setup.

UPDATE As per @Cheng 's comment I am attaching the lines from apache access log

127.0.0.1 - - [27/May/2015:18:36:16 +0530] "GET /static/admin/css/base.css HTTP/1.1" 404 512 "http://localhost/admin/login/?next=/admin/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0"
127.0.0.1 - - [27/May/2015:18:36:16 +0530] "GET /static/admin/css/login.css HTTP/1.1" 404 514 "http://localhost/admin/login/?next=/admin/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0"

WORKING SOLUTION Thanks to @Cheng, I got a solution to the problem. I further read in the docs to confirm the solution.

Note that if you include a trailing / on the URL-path then the server will require a trailing / in order to expand the alias. Likewise, if you omit the slash on the URL-path then you must also omit it from the file-path

If you closesly observe the snippet I had provided, I had missed the trailing slash in the file path.

Community
  • 1
  • 1
rahulmishra
  • 620
  • 1
  • 13
  • 29
  • 3
    This seems more like a Apache config issue rather than a Django issue. After you collected the static files, can you find the css files for the admin page being copied to the static directory? Also, try to access the admin static files in your browser like `www.yoursite.com/static/whatever.css`, what error code do you get? – Cheng May 27 '15 at 07:14
  • On the same topic: http://stackoverflow.com/questions/9500598/apache-not-serving-django-admin-static-files – Freek Wiekmeijer May 27 '15 at 07:24
  • @Cheng yes, I can find the files in the static directory. Also, accessing it directly with browser gives a 404 – rahulmishra May 27 '15 at 07:59
  • @FreekWiekmeijer , I have already implemented the solutions given on the link you have provided, and it is visible in the code snippets shown. – rahulmishra May 27 '15 at 08:01
  • You don't have `ADMIN_MEDIA_PREFIX` set in your settings.py right? Also, check your access_log to see what the log says and please post that line of log. Another thing that I can think of is to link to django's files directly `Alias /static /usr/lib/python2.6/site-packages/django/contrib/admin/static` (which has been suggested by many others) make sure you adjust the path and see if this works. – Cheng May 27 '15 at 08:10
  • @Cheng I have update my answer with the information you sought. Changing the alias however did not solve the problem. Also the ADMIN_MEDIA_PREFIX setting is no longer required in Django 1.8 – rahulmishra May 27 '15 at 13:11
  • I guess we have to check the problem one step at a time. Do you have an error log file as well. Do you see any errors in the error log? You said changing the alias did not work, you mean you changed it to `/usr/lib/python2.6/site-packages/django/contrib/admin/static` – Cheng May 27 '15 at 13:16
  • No, there is no related entry in the error log. I am using virtualenv for my django app, so I have pointed the alias correspondingly. – rahulmishra May 27 '15 at 13:23
  • @Cheng, your solution did work with a slight modification from the answer by OP here http://stackoverflow.com/questions/25320893/django-1-6-serving-static-admin-files-using-alias-with-apache?rq=1 . I cannot however figure out what was wrong with earlier setup – rahulmishra May 27 '15 at 13:24
  • For the error log, I mean apache's error log. You should see something there that is related to this 404 you are getting. Could you also post your solution by updating this question? – Cheng May 27 '15 at 13:40
  • @Cheng , I have updated my answer, thanks for the help :) – rahulmishra Jun 01 '15 at 11:26
  • @light94 glad to help :) – Cheng Jun 02 '15 at 09:13

0 Answers0