SoFunction
Updated on 2024-11-10

Solve the problem of xadmin backend management system style loss when Django deployment settings Debug=False

For systems developed using the Django framework, xadmin backend management system styles are lost when Debug=False is set in the file during deployment.

[Cause of the problem]:

The production environment of django is different from the development environment, in the production environment (DEBUG=False), it doesn't play any role, that is to say, only for the development environment (DEBUG=True) to open. So it will lead to xadmin style loss phenomenon.

[Solution]:

①Add the following configuration to

STATIC_ROOT = (BASE_DIR, 'static')

② Modify files in the main project

urlpatterns = [
 path('xadmin/', ),
 //...
 re_path('static/(?P<path>.*)', serve, {'document_root':STATIC_ROOT}),
]

③ Execute the following command in the console, it will generate the static file in the root directory, which stores the style files of xadmin:

> collectstatic

At this time Django's backend management system xadmin style back to the original ~!

Additional knowledge:django access xadmin can not load style problem

After xadmin was installed, I was surprised that there were no styles, checked the internet and found the following solutions:

1. Official website: replacing files under xadmin fails.

2. Modify, add STATIC_ROOT, failed.

STATIC_URL = '/static/'
#If you set it the same way, you'll be warned when you run collectstatic.
STATIC_ROOT = (BASE_DIR, 'static')

Neither of these worked for me, and I found this method on my own:

Modify the file,debug to true, successfully solved.

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

Above this article to solve the Django deployment set Debug=False when xadmin backend management system style is lost is all I share with you, I hope to be able to give you a reference, but also hope that you support me more.