PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 19, 2022

[FIXED] How to rename "admin" static files in Django?

 October 19, 2022     admin, django     No comments   

Issue

When loading up the Django admin pages, you load in the default Django admin css / js files. For example, example.com/staticfiles/admin/css/base.css.

I've enabled a default Amazon WAF (firewall) rule that blocks all access to pages that have the word "admin" in their URL. This has been great so far -- as most bots love looking for /admin domains.

However, this ends up blocking the Django admin. I'm able to rename the Django admin url to /manager using one line of code in urls.py, however I can't figure out how to to change the admin static files' filepath. I'm currently getting raw, unstyled HTML in my admin as all the static files are blocked.

Ideally, in settings.py I could write:

ADMIN_FOLDER_NAME = 'manager'

and then

example.com/staticfiles/admin/css/base.css would get rewritten to example.com/staticfiles/manager/css/base.css.

How can I tell Django to look in a different folder for admin files?

Note

I am not asking how to rename the url for accessing the admin page. I am talking about the admin static files. The below code does change the admin page url, but does not change the url for the staticfiles.

urlpatterns += [url(r'^newadminurl', admin.site.urls)]


Solution

As you may have noticed in example.com/staticfiles/admin/css/base.css, the url is based off of the folder structure.

Also, it should be noted that in django admin templates, static file paths are said to look into a folder named admin like so: {% static "admin/css/base.css" %} (admin static file paths are hard-coded).

# a line from admin/base.html
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">

This means that you cannot change the path after example.com/staticfiles/ for admin static files.

But, can be done by copying the templates directory

  • Copy templates directory from venv/Lib/site-packages/django/contrib/admin/ to project base folder
  • Find all the occurrences of {% static "admin/ and replace {% static "admin/ with {% static "manager/ in the templates. Need to be careful not to change any other thing in the template files.
  • Update TEMPLATES settings > 'DIRS': [os.path.join(BASE_DIR, 'templates')]

Overriding Admin Templates


Another possible way to achieve this could be rewriting the URL to remove or replace admin from the URL. Simply rewriting the URL would result in 404. Furthermore, server configuration for serving the admin static files has to be additionally added.



Answered By - Achuth Varghese
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing