本文介绍了缺少Django Admin CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用新的 collectstatic 命令,并让它正常工作在我的普通页面。也就是说,我可以在这个位置加载我的css http:// localhost:8000 / static / css / main.css 。但是,我的django管理员的css似乎没有显示出来。



当导航到管理员css位置 http:/ /localhost:8000/static/admin/css/base.css ,我收到一个404页面,找不到以下错误: / home / nai / GitProjects / cats /django-trunk/django/contrib/admin/media/css/base.css不存在。查看django-trunk,我从来没有过$ / home / nai / gitProjects / cats / django-trunk / django / contrib / admin / media / 文件夹开始。



这很奇怪吗? p>

无论如何,在我的静态文件夹中,有一个管理文件夹,其中附带了css,img和js文件夹,当我运行collectstatic和base的url时创建。 css似乎指向该位置。



这是在我的django开发服务器上发生的,这里有一些帮助狩猎的代码片段:



urls

  33#为了让Dev Server服务米edia文件为前端站点。 
34 url​​patterns + = staticfiles_urlpatterns()
35
36 try:
37 if settings.DEBUG:#在第一个arg为runserver时在manage.py中定义
38 urlpatterns + = pattern('',
39(r'^ media /(?P< path>。*)$','django.views.static.serve',{'document_root' MEDIA_ROOT}),
40(r'^ media-admin /(?P< path>。*)$','django.views.static.serve',{'document_root':os.path.join设置.MEDIA_ROOT,'..',settings.ADMIN_MEDIA_PREFIX)}),
41)
42除了NameError:
43 pass

我认为这可能与我的URLS文件中的第40行有关,但将media-admin更改为static / admin没有帮助。



设置

  58 ADMIN_MEDIA_PREFIX ='/ static / admin'
69 STATIC_ROOT = os.path.join(os.path.abspath(os.path.join(PROJECT_ROOT,'..',MEDIA_DIR,'static')),'')
70
71#静态文件的URL前缀。
72#示例:http://media.lawrence.com/static/
73 STATIC_URL ='/ static /'
74
75#静态文件的附加位置。全局文件存储在这里
76 STATICFILES_DIRS =(
77 os.path.join(os.path.abspath(os.path.join(PROJECT_ROOT,'..','proj_public','static ','proj'))''),
78)
79


解决方案

在Django 1.4 ADMIN_MEDIA_PREFIX 已被弃用。以下是我遵循的步骤,以追踪这些最近的Django更改:


  1. 设置中。 py ,将 django.contrib.staticfiles 添加到 INSTALLED_APPS


  2. settings.py define STATIC_URL - staticfiles应用程序不会没有它运行。在使用 runserver 时,他们会被神奇地处理,但是当您部署时,这需要一个位置,这些资源可以通过浏览器获取。


我认为这是所有的。


I've been messing around with the new collectstatic command and have got it working for my normal pages. That is to say, I am able to load my css at this location http://localhost:8000/static/css/main.css. However, the css for my django admin doesn't seem to be showing up.

When I navigate to the admin css location at http://localhost:8000/static/admin/css/base.css, I'm getting a 404 page not found with the following error: /home/nai/GitProjects/cats/django-trunk/django/contrib/admin/media/css/base.css" does not exist. Looking in django-trunk, I never had the /home/nai/GitProjects/cats/django-trunk/django/contrib/admin/media/ folder to begin with.

Is that weird?

In any case, in my static folder, there is an admin folder with the accompanying css, img and js folders which was created when I ran collectstatic and the url of the base.css seems to be pointing to that location.

This is happening on my django development server. Here are some snippets to aid in the bug hunt:

urls

 33 # In order for Dev Server to serve media files for the frontend site.
 34 urlpatterns += staticfiles_urlpatterns()
 35
 36 try:
 37     if settings.DEBUG: # defined in manage.py when the first arg is "runserver"
 38         urlpatterns += patterns('',
 39         (r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
 40         (r'^media-admin/(?P<path>.*)$', 'django.views.static.serve',{'document_root': os.path.join(settings.MEDIA_ROOT, '..', settings.ADMIN_MEDIA_PREFIX)}),
 41         )
 42 except NameError:
 43     pass

I think it might be something to do with line 40 in my URLS file but changing media-admin to static/admin didnt help.

settings

     58 ADMIN_MEDIA_PREFIX = '/static/admin'
     69 STATIC_ROOT = os.path.join(os.path.abspath(os.path.join(PROJECT_ROOT, '..', MEDIA_DIR, 'static')), '')
     70
     71 # URL prefix for static files.
     72 # Example: "http://media.lawrence.com/static/"
         73 STATIC_URL = '/static/'
     74
     75 # Additional locations of static files. Global files are stored in here
     76 STATICFILES_DIRS = (
     77     os.path.join(os.path.abspath(os.path.join(PROJECT_ROOT, '..', 'proj_public', 'static', 'proj')), ''),
     78     )
     79
解决方案

In Django 1.4 ADMIN_MEDIA_PREFIX is deprecated. Here are the steps I followed to catch up with these somewhat recent Django changes:

  1. in settings.py, add django.contrib.staticfiles to INSTALLED_APPS

  2. in settings.py define STATIC_URL — the staticfiles app won't run without it. While using runserver they'll get handled magically, but when you deploy, this needs to be a location where those resources can be fetched by a browser.

I think that's all there was to it.

这篇关于缺少Django Admin CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:50