标签:lap blog isp project http close 配置信息 published width
一、django的安装
1.先安装python
2.再安装django:pip install django
3.使用虚拟环境:pip install virtualenv
二、项目的创建
1.可以先创建一个虚拟目录
创建虚拟目录:
virtualenv --no-site-packages venv
进入虚拟目录:
source venv/bin/activate
2.创建一个工程:
创建:
jango-admin.py startproject projectName
目录结构大致如下:
启动服务:
python manage.py runserver 0.0.0.0:8000
3.创建app:
进入projectName目录下运行:python manage.py startapp appName
目录结构大致如下:
4.创建模型:
1.编辑model.py文件:如添加如下代码
1 from django.db import models 2 from django.utils import timezone 3 import datetime 4 # Create your models here. 5 6 7 class Question(models.Model): 8 question_text = models.CharField(max_length=200) 9 pub_date = models.DateTimeField(‘date published‘) 10 11 def __str__(self): # __unicode__ on Python 2 12 return self.question_text 13 14 def was_published_recently(self): 15 return self.pub_date >= timezone.now() - datetime.timedelta(days=1) 16 was_published_recently.admin_order_field = ‘pub_date‘ 17 was_published_recently.boolean = True 18 was_published_recently.short_description = ‘Published recently?‘ 19 20 21 class Choice(models.Model): 22 question = models.ForeignKey(Question) 23 choice_text = models.CharField(max_length=200) 24 votes = models.IntegerField(default=0) 25 26 def __str__(self): # __unicode__ on Python 2 27 return self.choice_text
注:类中相应的属性,对于数据库中表的相应列名
2.编辑setting.py文件:
1)修改数据库配置信息:(根据自己的数据库配置)
DATABASES = {
‘default‘:{
‘ENGINE‘:‘django.db.backends.mysql‘,
‘NAME‘:‘pythontest‘,
‘USER‘:‘root‘,
‘PASSWORD‘:‘root‘,
‘HOST‘:‘localhost‘,
‘PORT‘:‘3306‘,
}
}
2)修改INSTALLED_APPS,添加app:
INSTALLED_APPS = [
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
‘appName‘,
]
3.同步数据库:(项目根目录下执行)
python manage.py makemigrations polls
python manage.py migrate
标签:lap blog isp project http close 配置信息 published width
原文地址:http://www.cnblogs.com/fqfanqi/p/7482466.html