How to Solve Cannot Load Django Admin Static Files when using Additional URL or ProxyPass Access

Posted on

Introduction

This article will show how to solve a certain problem. The problem appear when the accessing the admin page of a Django-based application. It is running using a non regular URL address. Basically, in general the application will run in an URL address for an example ‘http://www.mydjangoapp.com’. But in this context, it will in an URL address with an additional URL in it for an example ‘http://www.mydjangoapp.com/myapp’. So, there is an additional URL address part ‘/myapp’. In this condition, loading the admin page will be a problem. Because the loading process of the admin static files will end in a failure. The following is the image describing the condition :

How to Solve Cannot Load Django Admin Static Files when using Additional URL or ProxyPass Access
How to Solve Cannot Load Django Admin Static Files when using Additional URL or ProxyPass Access

Apparently, the problem for accessing the admin page is only the matter of the failure for loading every static files needed. Including stylesheet files, javascript files, image files and many other types.

Solution

This part will give a manual solution to solve the problem. Actually, the solution is very simple. In this case, just copy the admin static files available in the library of the python used in the operating system. In this context, fortunately there is a specific python virtual environment. So, the following are the steps :

  1. Check the file where the python library exist. In this case it is a python library which exist in a virtual python environment. The virtual python environment exist by the command execution of ‘pip -m venv env’. Look the article with the title of ‘How to Create a Python Virtual Environment in Microsoft Windows’ in this link. Another example exist in the article with the title of ‘How to Create a Python Virtual Environment’ in this link.

  2. In this context, the folder exist in the project of the Django folder. It is exist with the name of ‘env’. So, search the admin staticfiles in it. Apparently, it exist in the following path :

    root@localhost:~/project/env/lib/python3.8/site-packages/django/contrib/admin/static/admin$ ls
    css  fonts  img  js
    root@localhost:~/project/env/lib/python3.8/site-packages/django/contrib/admin/static/admin$
    
  3. So, just copy the admin folder with all of the content inside of it. Put it into the staticfiles folder. In this context, the staticfiles folder is in the root folder of the project. It is very clear since it is exist in the definition of the staticfiles folder lcoation inside the ‘settings.py’ file. The ‘settings.py’ file which is part of the project source code contains the following line for defining the staticfiles folder location :

    STATIC_ROOT = os.path.join(CORE_DIR, 'staticfiles')
  4. So, copy the admin folder to the staticfiles folder of the project as follows :

    root@localhost:~/project/env/lib/python3.8/site-packages/django/contrib/admin/static/admin$cd ..
    root@localhost:~/project/env/lib/python3.8/site-packages/django/contrib/admin/static$ cp -r admin/ /home/user/django/project/staticfiles/
    root@localhost:~/project/env/lib/python3.8/site-packages/django/contrib/admin/static$ cd  /home/user/django/project/staticfiles
    root@localhost:~/django/project/staticfiles$ ls -l 
    admin assets
    root@localhost:~/django/project/staticfiles$
    
  5. Try to access the admin page once more, if there are no other error message appear, the admin page will appear normally.

Leave a Reply