本讲演示简单使用 Django Admin 功能。

一,修改 settings.py,添加 admin 应用:

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)

二,修改 urls.py,添加 /admin 映射:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'csvt03.views.home', name='home'),
# url(r'^csvt03/', include('csvt03.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^index/$','blog.views.index'),
)

三,修改 blog/models.py 加入 User 数据表后同步数据库:

from django.db import models

class Employee(models.Model):
name = models.CharField(max_length=20) # map 'name' field to db def __unicode__(self):
return self.name class Entry(models.Model):
name = models.CharField(max_length=20) def __unicode__(self):
return self.name class Blog(models.Model):
name = models.CharField(max_length=20)
entry = models.ForeignKey(Entry) # Many-To-One Map: Blogs - Entry def __unicode__(self):
return self.name sex_choices = {
('F', 'Female'),
('M', 'Male')
} class User(models.Model):
name = models.CharField(max_length=20)
sex = models.CharField(max_length=1, choices=sex_choices)
[root@bogon csvt03]#  python manage.py syncdb
Creating tables ...
Creating table blog_user
Creating table django_admin_log
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

四,在 blog/ 下创建 admin.py,注册 User 表,以供 Django-Admin 对 User 表的管理:

from django.contrib import admin
from blog.models import User admin.site.register(User)

五,打开开发服务器 python manage.py runserver 之后,在 http://127.0.0.1:8000/admin 中可以对用户及数据库进行方便管理。

django: db - admin的更多相关文章

  1. 使用django执行数据更新命令时报错:django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency users.00 01_initial on database 'default'.

    如果在重新封装更新用户表之前,已经更新了数据表,在数据库中已经有了django相关的依赖表,就会报错: django.db.migrations.exceptions.InconsistentMigr ...

  2. django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applie

    Traceback (most recent call last): File "manage.py", line 15, in <module> execute_fr ...

  3. 替换django的user模型出现的异常django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency users.0001_initial on database 'default'

    django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applie ...

  4. Django db relationship

    # coding=utf-8 from django.db import models """ Django数据库关系: 一对一关系:OneToOneField 多对多关 ...

  5. django: db - display

    本讲介绍数据在页面中的呈现,内容很简单,就是嵌套循环在模板中的使用. 一,修改 csvt03/urls.py: from django.conf.urls import patterns, inclu ...

  6. django: db howto - 1

    以在 Django 中使用 MySQL 为例,首先要安装 MySQL 和 MySQL-python 组件,确保 python 能执行 import MySQLdb. MySQL 中创建数据库: [ro ...

  7. Django中Admin样式定制

    Django自带的admin在展示数据是样式有点单一,我们可以自己定义数据的展示样式. 一.自定义数据展示样式 1.后台查询书记列表时,同时列出出版社和出版时间: admin.py文件 from dj ...

  8. 报错django.db.migrations.exceptions.InconsistentMigrationHistory

    Pycharm强大的功能总是让我很是着迷,比如它的makemigrations 和 migrate. 然而某一次,当我再次敲下这熟悉的命令时,它报错了.... Traceback (most rece ...

  9. Django之admin的使用和源码剖析

    admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLE ...

随机推荐

  1. linux笔记2.19

    压缩一般使用 tar -cvzf etcbackup.tar.gz  /etc 寻找文件 locate(快速查找:新添加的得用updatedb更新后才能找到) find:   find . -name ...

  2. Jquery插件模版

    ;(function($){ $.fn.jcDate = function(options) { var defaults = { IcoClass : "jcDateIco", ...

  3. 函数式编程做用户登陆注册练习-pycharm上

    def login(username,password): """ 用户登陆 :param username: 用户名 :param password:密码 :retur ...

  4. template of class

    class template will call the constructor of its member object before constructor itself......

  5. myEclipse8.* 手动安装ADT插件[转]

    Myeclipse8.6手动添加插件: 自动在线安装完ADT插件会导致myeclipse8.6中无法创建web项目以及其他项.解决方法是手动安装步骤如下: 首先安装好 MyEclipse 8.6. 1 ...

  6. Decimal Basic 学习笔记(1)

    定义变量 LET a 输入变量值 INPUT a INPUT a,b 运算结果绝对值小于1前面的0省略,科学计数 PRINT语句 数值直接写,字符串用“” 通过 分号: 和 逗号,来分隔显示两个项目 ...

  7. CentOS下安装postgresql

    一.说明 postgresql版本:9.4.1 安装包: postgresql94-server-9.4.1-1PGDG.rhel6.x86_64.rpm postgresql94-libs-9.4. ...

  8. Android Activity整体管理和关闭工具类封装

    如何彻底退出程序,在任意位置退出程序,如何管理当前的运行栈,知道activity的堆栈结构等,本文封装了一个Activity管理类,可以方便随时退出程序. /** * 应用程序Activity管理类: ...

  9. AngularJs中文社区学习资料

    AngularJs中文社区学习资料,供学习: http://angularjs.cn/tag/AngularJS

  10. ASP.NET MVC 3 Razor Nested foreach with if statements

    You need to write code this way. @Html.Raw("<tr>") Copy the below code and paste it ...