标签:sage als rate images connect cat display tom sql
运行命令mysql -uroot -p进入mysql
新建一个数据库mydatabase
在/mysite/mysite目录下 编辑settings.py文件:
默认数据库为sqlite:
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.sqlite3‘, ‘NAME‘: ‘mydatabase‘, } }
mysql数据库配置:
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘mydatabase‘, ‘USER‘: ‘root‘, ‘PASSWORD‘: ‘123‘, ‘HOST‘: ‘127.0.0.1‘, ‘PORT‘: ‘3306‘, } }
启动django:python manage.py runserver
在新的shell运行:python manage.py runserver
from django.db import connection
cursor = connection.cursor()
没有报错 表示配置完成
建立新的一个app 运行命令:
python manage.py startapp myapp
进去myapp目录 编辑 models.py文件
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models # Create your models here. class Publisher(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) state_province = models.CharField(max_length=30)#default=‘CN‘ country = models.CharField(max_length=50) website = models.URLField() def __unicode__(self): return self.name class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField(blank=True) def __unicode__(self): return self.last_name class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField() def __unicode__(self): return self.title
在/mysite/mysite目录下编辑settings.py文件 添加app
INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘myapp‘,
依次运行下面两条命令 创建表
python manage.py makemigrations
python manage.py migrate
创建站点用户:
python manage.py createsuperuser
启动django:pyhon manage.py runserver 0.0.0.0:9999
这样就可以在浏览器上登录站点 http://192.168.177.130:9999/admin
输入刚才创建的用户和密码进入进行管理:
在/mysite/myapp目录下编辑admin.py文件 就会在站点页面中出现:
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.contrib import admin from myapp.models import Publisher,Author,Book # Register your models here. class Bookadmin(admin.ModelAdmin): search_fields = (‘title‘,)#查询 filter_horizontal = (‘authors‘,)#横向查询选择 #filter_vertical = (‘authors‘,)纵向 list_display = (‘title‘,‘publisher‘,‘publication_date‘)#显示其他信息功能 list_filter = (‘title‘,‘publisher‘,‘publication_date‘)#右边分类功能 ordering = (‘-publication_date‘,)#其他信息的排序功能 #fields =(‘title‘) 排序功能 admin.site.register(Publisher) admin.site.register(Author) admin.site.register(Book,Bookadmin)
可以在页面上添加表数据
下图是在Book中添加的各种功能
标签:sage als rate images connect cat display tom sql
原文地址:http://www.cnblogs.com/xiaoxinzang/p/7296038.html