万里长征第二步——django个人博客(第五步 ——配置后台admin)
- 在urls.py文件中配置admin路径
from django.conf.urls import url from django.contrib import admin from blog.views import index urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', index, name='index') ]
- 将需要在后台显示的数据库配置在admin.py文件中
from django.contrib import admin from models import * # Register your models here. admin.site.register(User) admin.site.register(Tag) admin.site.register(Article) admin.site.register(Catagory) admin.site.register(Comment) admin.site.register(Links) admin.site.register(Ad)
- 在命令行输入 'manage.py creatsuperuser '创建一个后台的超级管理员账号
- manage.py runserver,将本地服务器运行起来
(blog_project_venv) d:\python\blog_project>manage.py runserver Performing system checks... 2016-05-26 23:34:24,711 [Dummy-1:13568] [django.db.backends:89] [utils:execute] [DEBUG]- (0.000) SET SQL_AUTO_IS_NULL = 0; args=None System check identified no issues (0 silenced). 2016-05-26 23:34:24,779 [Dummy-1:13568] [django.db.backends:89] [utils:execute] [DEBUG]- (0.000) SET SQL_AUTO_IS_NULL = 0; args=None 2016-05-26 23:34:24,779 [Dummy-1:13568] [django.db.backends:89] [utils:execute] [DEBUG]- (0.000) SHOW FULL TABLES; args=None 2016-05-26 23:34:24,779 [Dummy-1:13568] [django.db.backends:89] [utils:execute] [DEBUG]- (0.000) SELECT `django_migrations`.`app`, `django_migrations`.`name` FROM `django_migrations`; args=() May 26, 2016 - 23:34:24 Django version 1.9.6, using settings 'blog_project.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
- 在浏览器中输入配置好的路径 ‘127.0.0.1:8000/admin’进入admin后台管理界面,输入创建好的超级管理员账号,进入后台。
- 可以在admin.py中对后台进行各种配置
# -*- coding=utf-8 -*- from django.contrib import admin from models import * # Register your models here. class ArticleAdmin(admin.ModelAdmin): #自定义个文章类 list_display = ('title','desc','click_count',) #让这些数据在后台默认显示 list_display_links = ('title','desc',) #让这些数据可以连接修改 list_editable = ('click_count',) #让‘click_count' 可以直接修改 # fields = ('title','desc','content') #让‘文章’在后台只显示‘title’,‘desc',‘content’选项 #exclude = ('title','desc','content') #让‘文章’在后台不显示‘title’,‘desc',‘content’选项 # fieldsets = ( #将‘文章’分类显示 # (None,{ #将要显示的数据 # 'fields': ('title', 'desc', 'content') # }), # ('高级设置', { # 'classes': ('collapse',), #将数据显示隐藏 # 'fields': ('click_count', 'is_recommend') # }), # ) admin.site.register(User) admin.site.register(Tag) admin.site.register(Article ,ArticleAdmin) #将ArticleAdmin添加进后台 admin.site.register(Catagory) admin.site.register(Comment) admin.site.register(Links) admin.site.register(Ad)