本讲演示简单使用 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. Leetcode 371: Sum of Two Integers(使用位运算实现)

    题目是:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...

  2. MySQL Connector/Python 安装、测试

         安装Connector/Python: # wget http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-pyth ...

  3. postgres安装 以及修改postgres 密码

    #postgres安装 apt-get install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 postgresql-s ...

  4. 淘宝接口实现ip归属地查询

    <?php header('content-type:text/html;charset=utf-8'); /*获取当前ip归属地 调用淘宝接口 */ function get_ip_place ...

  5. 机器学习之python: kNN

    ################################################## # kNN : k Nearest Neighbour # Author : Monne # Da ...

  6. OpenStack JEOS 镜像

    JEOS:Just Enough Operating System 维基百科地址:http://en.wikipedia.org/wiki/Just_enough_operating_system O ...

  7. ISAP

    跑的是比Dinic快辣. 更新:指针版.... #include<iostream> #include<cstdio> #include<cmath> #inclu ...

  8. iOS 9之WatchKit for WatchOS 2

    金田(github示例源码) 自AppleWatch发行的同时就可以为AppWatch开发相应的应用程序,不过最初的版本,能开发的功能极为有限,所以也只是有少数的App厂商为Apple定制了App,所 ...

  9. SPOJ694 -- DISUBSTR 后缀树组求不相同的子串的个数

    DISUBSTR - Distinct Substrings   Given a string, we need to find the total number of its distinct su ...

  10. [饭后算法系列] "头尾移动" 排序列表

    1. 问题 一个乱序列表(list), 只支持两种操作: 把一个元素移动到头部, 或者把一个元素移动到尾部. 需要设计一种算法, 使得移动次数最少而使列表有序 举两个例子: 1. {3,5,7,1,9 ...