开始配置django
做好前期的安装工作之后,开始了
1.配置setting
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: “hotel”,
‘USER’:‘root’,
‘PASSWORD’:‘password’,
‘HOST’:‘127.0.0.1’
}
首先配置好这里
在mysql中建立一个叫hotel的库
然后run manage.py task
makemigrations
migrate
数据表就生成完毕
run一下,可以在127.0.0.1:8000看到django运行成功了
2.新建app
run manage.py task
startapp user
第一步就是编写model
from django.contrib.auth.models import AbstractUser
class UserProfile(AbstractUser):
nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
birday = models.DateField(verbose_name=u"生日", null=True, blank=True)
gender = models.CharField(max_length=6, choices=((“male”, u"男"), (“female”, “女”)), default=“female”)
address = models.CharField(max_length=100, default=u"")
mobile = models.CharField(max_length=11, null=True, blank=True)
image = models.ImageField(upload_to=“image/%Y/%m”, default=u"image/default.png", max_length=100)
class Meta:
verbose_name = "用户信息"
verbose_name_plural = verbose_name
def __unicode__(self):
return self.username
设计好model之后,在setting中的apps中注册users
并且加上
AUTH_USER_MODEL = “users.UserProfile”
然后开始生成表,以后说的生成表,一律是使用makemigration 和migrate 不再赘述
生成表成功
类似的
生成room(房间表)
from django.db import models
from datetime import datetime
class Room(models.Model):
RoomNumber = models.IntegerField(default=0,verbose_name=u"房间号")
price = models.IntegerField(default=0,verbose_name=u"价格")
type = models.CharField(max_length=10,default=“客房”,verbose_name=u"类型")
status = models.CharField(choices=((“waiting”, u"预定"), (“check”, u"入住")), max_length=10,default=“waiting”)
desc = models.TextField(verbose_name=u"简介")
add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间")
class Meta:
verbose_name = u"房间信息"
verbose_name_plural = verbose_name
生成酒店表
from django.db import models
from datetime import datetime
class myhotel(models.Model):
hotel_id = models.IntegerField(default=-1,verbose_name=u"酒店号")
name = models.CharField(max_length=20,default=u"我的酒店名称",verbose_name=u"酒店名称")
desc = models.TextField(verbose_name=u"简介")
say = models.TextField(verbose_name=u"评论")
year = models.IntegerField(verbose_name=u"装修年份",default=1900)
add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
class Meta:
verbose_name = u"酒店信息"
verbose_name_plural = verbose_name
生成vip表
from django.db import models
from datetime import datetime
class Vip(models.Model):
vip_id = models.IntegerField(default=0,verbose_name=“会员号”)
vip_level = models.IntegerField(default=0,verbose_name=“会员等级”)
vip_off = models.IntegerField(default=1, verbose_name=“折扣比率”)
money_in = models.IntegerField(default=0,verbose_name=“累充金额”)
money_less = models.IntegerField(default=0,verbose_name=“剩余金额”)
add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
class Meta:
verbose_name = "会员信息"
verbose_name_plural = verbose_name
将这些app都注册到setting中去
新建一个python package 叫apps
将上面的app都拖到里面去,然后mark成资源根目录,在setting中的
BASE_DIR下面加上一行
sys.path.insert(0,os.path.join(BASE_DIR,“apps”))
然后可以运行成功
在cmd,打开虚拟环境,切换到项目的目录
python manage.py runserver 0.0.0.0:8000
就可以在自己的ip地址:8000打开diango了
还有一个操作app, 管理预定,入住,换房,续住,充值的app,编写一系列model,然后生成表
from django.db import models
from datetime import datetime
from users.models import UserProfile
from room.models import Room
class UserInRoom(models.Model):
#“预定入住表”
user_id = models.IntegerField(verbose_name=u"客户号", default=0)
name = models.CharField(max_length=10,verbose_name=u"姓名",default="")
phone = models.CharField(max_length=11,verbose_name=u"联系电话",default="")
idnum = models.CharField(max_length=30,verbose_name=u"身份证号",default="")
arrivetime = models.DateTimeField(default=datetime.now,verbose_name=u"到店时间")
onmoney = models.IntegerField(default=100,verbose_name=“押金”)
ticket = models.CharField(choices=((1,u"是"),(2,u"否")),default=2,verbose_name=u"开具发票",max_length=5)
check = models.CharField(choices=((1,u"是"),(2,u"否")),default=2,verbose_name=u"确认入住",max_length=5)
add_time = models.DateTimeField(default=datetime.now,verbose_name=u"订单时间")
class Meta:
verbose_name = u"预定入住信息"
verbose_name_plural = verbose_name
class UserInMoney(models.Model):
user_id = models.IntegerField(verbose_name=u"客户号", default=0)
money = models.IntegerField(default=0,verbose_name=u"充值金额")
class Meta:
verbose_name = u"充值信息"
verbose_name_plural = verbose_name
class UserChangeRoom(models.Model):
user_id = models.IntegerField(verbose_name=u"客户号", default=0)
RoomNumber = models.IntegerField(default=0, verbose_name=u"房间号")
to_price = models.IntegerField(default=0,verbose_name=u"差价")
type = models.CharField(max_length=10, default=u"客房", verbose_name=u"类型")
status = models.CharField(choices=((“waiting”, u"预定"), (“check”, u"入住")), max_length=10, default=“waiting”)
change_time = models.DateTimeField(default=datetime.now,verbose_name=u"换房时间")
class Meta:
verbose_name = u"换房信息"
verbose_name_plural = verbose_name
class UserContinueRoom(models.Model):
user_id = models.IntegerField(verbose_name=u"客户号", default=0)
RoomNumber = models.IntegerField(default=0, verbose_name=u"房间号")
time = models.IntegerField(default=0,verbose_name=u"续住时间")
add_time = models.DateTimeField(default=datetime.now , verbose_name=u"换房时间")
class Meta:
verbose_name = u"续住信息"
verbose_name_plural = verbose_name
如果migrate 失败了,加上参数 --fake
–fake 的含义是不执行该迁移脚本但是标记该脚本已经被执行过
如果今后要是不行,就要
SELECT * FROM django_migrations
发现将要执行的迁移脚本的 id 已经添加在表中了,将其删除即可。
将xadmin的源码包放到:
在根目录下创建一个文件夹etc_app,mark为资源的根目录
讲xadmin的整个文件夹放进去
在setting中注册xadmin
还有crispy_forms
在url中import xadmin
将下面改成
urlpatterns = [
url(r’^xadmin/’, xadmin.site.urls),
]
然后启动还是不能成功
将xadmin的表生成一下
然后可以登录了
不能上127.0.0.1:8000
得上127.0.0.1:8000/xadmin
将页面设置为简体中文的办法
其中 zh-Hans是简体中文 zh-Hant是繁体中文
所以更改setttings.py 下
LANGUAGE_CODE = ‘zh-hans’
TIME_ZONE = ‘Asia/Shanghai’
USE_I18N = True
USE_L10N = True
USE_TZ = False
然后新建超级用户,在Task那里