python djiago创建项目

2017-07-16  本文已影响858人  清风沐沐

1.进入Python虚拟环境

source  xxx/bin/activate

2.进入当前用户有操作权限的目录:

django-admin startproject First_Django

3.查看新建的项目内部文件

//进入First_Django项目
cd  First_Django
//查看项目的子目录文件
ls  
> First_Django(此子目录名称与项目名称相同,作用是配置项目)    manage.py (此文件用来管理项目)
// 进入First_Django项目的First_Django子目录
cd  First_Django
// 查看First_Django项目的First_Django子目录文件
> __init__.py settings.py urls.py     wsgi.py

整个项目文件结构:
|____First_Django
| |____First_Django
| | |______init__.py
| | |____settings.py
| | |____urls.py
| | |____wsgi.py
| |____manage.py

4.创建应用:

一个项目里可以创建一个或多个应用

// 在项目的目录下
Python manage.py startapp FirstApp

// 查看项目
ls 
> First_Django booktest(新建的项目)     manage.py

查看booktest文件结构:
 tree booktest/
>
booktest/
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py

5.编写代码

// models.py 中编写模型代码
class Bookinfo(models.Model):
    btitle = models.CharField(max_length=20)
    bpub_date = models.DateTiemField()
class HeroInfo(models.Model):
    hname=models.CharField(max_length=10)
    hgender=models.BooleanField()
    hcontent=models.CharFeild(max_length=1000)
    hbook=models.ForeignKey(Bookinfo)

6.跑起服务器

Python manage.py runserver

如果报错👇的错误:

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.
注册迁移:
在setting.py文件中的INSTALLED_APPS字段中添加项目名称:
执行迁移:
python manage.py migrate
启动服务器:
Python manage.py runserver

执行python manage.py migrate 如果报错👇的错误:

(FirstVirtualen) taoyali@taoyali-2:~/project/python3/First_Django$     python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

依次执行一下命令:

python manage.py makemigrations

python manage.py migrate

python manage.py runserver

运行效果:

(FirstVirtualen) taoyali@taoyali-2:~/project/python3/First_Django$     python manage.py makemigrations
Migrations for 'booktest':
  booktest/migrations/0001_initial.py
    - Create model BookInfo
    - Create model HeroInfo
(FirstVirtualen) taoyali@taoyali-2:~/project/python3/First_Django$     python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, booktest, contenttypes, sessions
Running migrations:
  Applying booktest.0001_initial... OK
(FirstVirtualen) taoyali@taoyali-2:~/project/python3/First_Django$     python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
July 16, 2017 - 06:06:04
Django version 1.11.3, using settings 'First_Django.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

7.测试

python manage.py shell 
//导入类
>>> from booktest.models import *
>>> b = BookInfo()
>>> b.btitle = 'abc'
//导入python时间
>>> from datetime import datetime
>>> b.bpub_date = datetime(year=1990,month=1,day=12)
>>> b.save()   //执行了数据库的insert操作
//打印对象信息
>>> BookInfo.objects.all()  //执行了数据库的select操作
<QuerySet [<BookInfo: BookInfo object>]>

>>> b = BookInfo.objects.last() //执行了数据库的select操作
>>> b.btitle = 'python'
>>> b.save()  //执行了数据库的update操作

>>> b.delete()  //执行了数据库的delete操作

上一篇下一篇

猜你喜欢

热点阅读