取消mysite/urls.py setting.py相关admin的注释,激活admin

 
解决nginx下admin管理界面丢失css的情况:
 
上网搜索了不少文档,多是Apache环境下的解决方案,我配置的是Nginx服务器,
 
所以根据Apache的解决提示进而摸索办法,最终要解决这个问题我这里总结有3步
 
(假设已经配置好admin登录系统):
 
 
1. 修改settings.py中STATIC_ROOT为你的static静态文件的物理路径,比如说我
 
静态文件存放在/home/user/www/static中,首先创建www目录下的static文件夹,
 
最后修改settings.py中STATIC_ROOT指向/home/user/www/static。
 
2. 运行python manage.py collectstatic命令,这将从Django资源包中复制必须
 
的静态文件到STATIC_ROOT指示的static文件夹中,这其中包括admin界面所必须的
 
样式表(style)、图片(p_w_picpath)及脚本(js)等。
 
这里提示一下,假如不做第1步的话,直接运行这个命令会导致如下的错误提示(
 
最后几行):
 
  File "/usr/lib/python2.7/dist-
 
packages/django/contrib/staticfiles/management/commands/collectstatic.py
 
", line 41, in __init__
    self.storage = get_storage_class(settings.STATICFILES_STORAGE)()
  File "/usr/lib/python2.7/dist-
 
packages/django/contrib/staticfiles/storage.py", line 23, in __init__
    raise ImproperlyConfigured("You're using the staticfiles app "
django.core.exceptions.ImproperlyConfigured: You're using the 
 
staticfiles app without having set the STATIC_ROOT setting.
其实已经提示需要配置STATIC_ROOT文件了(You’re using the staticfiles app 
 
without having set the STATIC_ROOT setting)。
 
3. 修改Nginx配置,Debian系统下/etc/nginx/nginx.conf或
 
者/etc/nginx/sites-enabled/里面的配置文件,在server节中添加如下内容(请
 
根据实际情况修改):
 
server {
  # ... 其余内容略
 
  location /static {
      root /home/user/www;
  }
 
}
此处不修改的话,我测试下来结果貌似默认处理会交由Django,但是依旧会由
 
Django提示Page not found (404)。
 
 
 
 
polls/admin.py
 
from django.contrib import admin
from polls.models import Poll
 
admin.site.register(Poll)
 
定制管理表单
class PollAdmin(admin.ModelAdmin):
    fields = [’pub_date’, ’question’]
admin.site.register(Poll, PollAdmin)
 
或者用fieldsets,将元素组框起来
class PollAdmin(admin.ModelAdmin):
fieldsets = [
(None, {’fields’: [’question’]}),
(’Date information’, {’fields’: [’pub_date’]}),
]
admin.site.register(Poll, PollAdmin)
 
最终:
from django.contrib import admin
from polls.models import Poll, Choice
 
 
class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3
 
class PollAdmin(admin.ModelAdmin):
#    fields = ['pub_date', 'question']
    fieldsets = [
        (None, {'fields':['question']}),
        ('Date information', {'fields': ['pub_date']}),
    ]
    inlines = [ChoiceInline]
 
admin.site.register(Poll, PollAdmin)
 
class PollAdmin(admin.ModelAdmin):
# 显示哪几列
list_display = (’question’, ’pub_date’, ’was_published_recently’)
 
 
在model增加列属性以便在admin中的列头显示名等更改
class Poll(models.Model):
# ...
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
was_published_recently.admin_order_field = ’pub_date’
was_published_recently.boolean = True
was_published_recently.short_description = ’Published recently?’
 
admin.py中管理器增强
增加日期选择date_hierarchy = ’pub_date’
增加搜索框search_fields = [’question’]
右侧增加日期选择l过滤list_filter = [’pub_date’]