本讲演示简单使用 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. C++采用模板实现栈的方法

    今天又看了遍<effective C++>,手动实现了一下条款42中的栈,贴出来当博客的处女贴. 首先栈的声明如下,采用了模板传入类型,而栈的底层采用是个链表. // stack.h // ...

  2. swf上传

    swfupload多文件异步上传 多文件选择异步上传的原理 传统上:多个文件逐一选.PHP开始处理,循环上 PHP+Flash上:JS调用flash控,Flash批量选取并保持选取所有文件列 swfu ...

  3. Swift —构造函数与存储属性初始化-备

    构造函数的主要作用是初始化实例,其中包括:初始化存储属性和其它的初始化.在Rectangle类或结构体中,如果在构造函数中初始化存储属性width和height后,那么在定义他们时就不需要初始化了. ...

  4. Spark 启动过程(standalone)

    Spark启动过程 正常启动Spark集群时往往使用start-all.sh ,此脚本中通过调用start-master.sh和start-slaves.sh启动mater及workers节点. 1. ...

  5. cf500B New Year Permutation

    B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. HDU_2057——64位无符号16进制数的运算

    Problem Description There must be many A + B problems in our HDOJ , now a new one is coming. Give yo ...

  7. HDU_2048——全错位排列递推公式

    Problem Description HDU 2006'10 ACM contest的颁奖晚会隆重开始了! 为了活跃气氛,组织者举行了一个别开生面.奖品丰厚的抽奖活动,这个活动的具体要求是这样的:首 ...

  8. 关于本学期西南交通大学ACM-ICPC校集训队 训练计划(Beta 1.0)

    在第十周新秀杯之后,从第十一周起的训练计划如下: 1.十一周的周一至周五进行ACM校集训队申请.申请方式从2014年11月17日0:00开始,发送申请者的姓名.学号.专业.电话.QQ以及大学(针对大一 ...

  9. openstack 手动 部署安装调试

    Virtual Interface creation failed

  10. hadoop2.2.0的WordCount程序

    package com.my.hadoop.mapreduce.wordcount; import java.io.IOException; import org.apache.hadoop.conf ...