1、运行
python manage.py runserver 0.0.0.0:8000
2、添加app
在pycharm的工具栏找到 Tools,点击之后会有“Run manage.py task”选项进入终端
startapp app_name
app添加到文件夹apps下
新建apps文件夹,markas sourse root
settings.py中
import os
import sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
3、生成默认数据库
在2的终端中:
makemigrations
migrate
makemigrations会记录修改的models.py,并且在migrations生成记录文件,此时数据库未作修改
migrate会把修改同步到数据库
4、添加 Django administration
python manage.py createsuperuser root root@qq.com rootroot
访问 http://127.0.0.1:8000/admin/ 进入
5、user继承AbstractUser
users app下models.py
class UserProfile(AbstractUser):
nickname = models.CharField(max_length=50, verbose_name=u"昵称")
birthday = models.DateField(max_length=50, null=True, blank=True, verbose_name=u"生日")
gender = models.CharField(choices=(("male", u"男"), ("female", u"女")), default=u"female", max_length=10)
address = models.CharField(max_length=100, default=u"")
mobile = models.CharField(max_length=11, null=True, blank=True)
image = models.ImageField(upload_to="images/upload/user/%y/%m", default=u"images/default/user_default.png", max_length=100) class Mate:
verbose_name = u"用户信息"
verbose_name_plural = verbose_name
ordering = ["-id"] def __unicode__(self):
return self.username
admin.py
from django.contrib import admin # Register your models here. from users.models import UserProfile class UserProfileAdmin(admin.ModelAdmin):
pass admin.
settings.py
AUTH_USER_MODEL = "users.UserProfile"
修改之后发现后台访问报错: 'WSGIRequest' object has no attribute 'user'
修改
MIDDLEWARE 改为 MIDDLEWARE_CLASSES
6、xadmin使用
pip install xadmin
pip list
settings.py中INSTALLED_APPS添加:
'xadmin',
'crispy_forms',
url.py中:
import xadmin urlpatterns = [
url(r'^xadmin/', xadmin.site.urls),
]
之前admin.py
from django.contrib import admin # Register your models here. # from users.models import UserProfile
#
#
# class UserProfileAdmin(admin.ModelAdmin):
# pass
#
# admin.site.register(UserProfile,UserProfileAdmin)
7、xadmin添加模块
在app下新建adminx.py代替admin.py
# coding=utf-8 import xadmin
from .models import EmailVerifyRecord class EmailVerifyRecordAdmin(object):
pass xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin)
这样就把models.py中的模块添加进后台管理
class EmailVerifyRecord(models.Model):
code=models.CharField(max_length=20,verbose_name=u"验证码")
email=models.EmailField(max_length=50,verbose_name=u"邮箱")
send_type=models.CharField(verbose_name=u"发送类型",choices=(("register",u"注册"),("forget",u"忘记密码"),("update_email",u"修改邮箱")),max_length=50)
send_time=models.DateTimeField(verbose_name=u"发送时间",default=datetime.now) class Meta:
verbose_name=u"邮箱验证码"
verbose_name_plural = verbose_name def __unicode__(self):
return '{0}({1})'.format(self.code,self.email)
用户模块自动会加入,不需要添加
8、定义列表显示的字段
修改以上xadmin.py的内容
class EmailVerifyRecordAdmin(object):
list_display = ['code', 'email', 'send_type', 'send_time']
9、定义搜索的字段
class EmailVerifyRecordAdmin(object):
...
search_fields = ['code', 'email', 'send_type', 'send_time']
10、添加过滤器增强搜索
class EmailVerifyRecordAdmin(object):
...
list_filter = ['code', 'email', 'send_type', 'send_time']