最近参加了胡阳老师的Django开发实战线上学习,现在总结下第一节学习的内容,是关于学生系统管理的开发
一、搭建开发环境
python3
venv是python3自带的创建虚拟环境模块
python -m venv djangp_test
在scripts文件下激活虚拟环境
activate
关闭虚拟环境
deactivate
C:\Users\ssaw
λ cd env
C:\Users\ssaw\Env
λ python -m venv django_test
C:\Users\ssaw\Env
λ cd django_test/scripts
C:\Users\ssaw\Env\django_test\Scripts
λ activate
C:\Users\ssaw\Env\django_test\Scripts
(django_test) λ deactivate
C:\Users\ssaw\Env\django_test\Scripts
λ
python2
python2是不能用venv来创建虚拟环境的,这里可以使用virtualenv来创建
python2 -m pip install virtualenv
C:\Users\ssaw
λ cd env
C:\Users\ssaw\Env
λ virtualenv django_test
New python executable in C:\Users\ssaw\Env\django_test\Scripts\python2.exe
Also creating executable in C:\Users\ssaw\Env\django_test\Scripts\python.exe
Installing setuptools, pip, wheel...done.
C:\Users\ssaw\Env
λ cd django_test/scripts
C:\Users\ssaw\Env\django_test\Scripts
λ activate
C:\Users\ssaw\Env\django_test\Scripts
(django_test) λ
二、创建项目
django-admin startproject django_student
C:.
├─.idea
│ django_test.iml
│ misc.xml
│ modules.xml
│ workspace.xml
│
└─django_student
│ manage.py
│
└─django_student
settings.py
urls.py
wsgi.py
__init__.py
安装Django模块
pip install django==1.11.2
运行下程序试试看
python manage.py runserver
(django_test) λ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 22, 2018 - 18:18:17
Django version 1.11.2, using settings 'django_student.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[22/Jan/2018 18:18:23] "GET / HTTP/1.1" 200 1716
浏览器打开http://127.0.0.1:8000/,出现It worked.........,说明运行成功

三、创建学生应用、管理后台admin
ctrl+c 关闭Web服务,输入以下命令来创建应用
python manage.py startapp student
(django_test) λ tree /f
文件夹 PATH 列表
卷序列号为 14B4-B57E
C:.
│ db.sqlite3
│ manage.py
│
├─django_student
│ settings.py
│ settings.pyc
│ urls.py
│ urls.pyc
│ wsgi.py
│ wsgi.pyc
│ __init__.py
│ __init__.pyc
│
└─student
│ admin.py
│ apps.py
│ models.py
│ tests.py
│ views.py
│ __init__.py
│
└─migrations
__init__.py
打开models.py
# student/models
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
class Student(models.Model):
SEX_ITEMS = [
(1, '男'),
(2, '女'),
(0, '未知'),
]
STATUS_ITEMS = [
(0, '申请'),
(1, '申请'),
(2, '拒绝'),
]
# CharField():数据类型,指定name为字符型, max_length:指定字符长度
# verbose_name: 可以理解为注释
name = models.CharField(max_length=128, verbose_name='姓名')
sex = models.IntegerField(choices=SEX_ITEMS, verbose_name='性别')
profession = models.CharField(max_length=128, verbose_name='职业')
email = models.EmailField(verbose_name='Email')
qq = models.CharField(max_length=128, verbose_name='QQ')
phone = models.CharField(max_length=128, verbose_name='电话')
home_phone = models.CharField(max_length=128, null=True, verbose_name='家庭电话')
status = models.IntegerField(choices=STATUS_ITEMS, verbose_name='审核状态')
# auto_now_add: 自动创建当前时间
# auto_now: 修改记录时间写进去
# editable: 设置为False,在admin模式下不能修改
created_time = models.DateTimeField(
auto_now_add=True,
editable=False,
verbose_name='创建时间')
# 返回字符串,在Python3中是__str__
def __unicode__(self):
return '<Student: {}>'.format(self.name)
class Meta:
verbose_name = verbose_name_plural = '学员信息'
打开admin.py
# student/admin.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
# 从admin统计目录下引用models模块
from .models import Student
class StudentAdmin(admin.ModelAdmin):
# 需要展示的列表
list_display = (
'id',
'name',
'sex',
'profession',
'email',
'qq',
'phone',
'status',
'created_time')
list_filter = ('sex', 'status', 'created_time')
search_fields = ('name', 'profession')
# fieldsets = (
# (None, {
# 'fields': (
# 'name',
# ('set', 'profession'),
# ('email', 'qq', 'phone'),
# 'status',
# )
# })
# )
# 把models下的Student和admin注册到一起
admin.site.register(Student, StudentAdmin)
接下来需要迁移数据库
python manage.py makemigrations
Migrations for 'student':
student\migrations\0001_initial.py
- Create model Student
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, student
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
Applying student.0001_initial... OK
在Django_student目录下打开setting.py把student应用添加进去
INSTALLED_APPS = [
'student',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
进入管理后台需要账号密码,现在我们来给自己创建一个用户账号,按步骤创建user,password,邮箱
python manage.py createsuperuser
再次开启Web服务,打开http://127.0.0.1:8000/admin/
python manage.py runserver
这个时候我们进入了admin后,发现是英文时间格式也是不对的,可以在setting.py中更改语言和时区
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True
初探Django的第一篇就完成了!!!
网友评论