Django的项目建立

系统:CentOS7

系统版本:7.6

内核版本:3.10.0-957.el7.x86_64

python版本:3.6.8

Django版本:2.1.10

一、创建Django项目

[root@zabbix 09:42:13/data/recycle/python]# django-admin startproject mysitel

二、安装mysql模块,python3已经不支持PyMySQL目录,所以安装pymysql

[root@zabbix 09:55:20/data/recycle/python/mysitel]# pip3 install pymysql
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pymysql
  Downloading https://files.pythonhosted.org/packages/4f/52/a115fe175028b058df353c5a3d5290b71514a83f67078a6482cff24d6137/PyMySQL-1.0.2-py3-none-any.whl (43kB)
    100% |████████████████████████████████| 51kB 301kB/s 
Installing collected packages: pymysql
Successfully installed pymysql-1.0.2
[root@zabbix 09:55:27/data/recycle/python/mysite1]# 

三、打开项目(mysitel)在setting.py同级别目录里的__init__.py文件,在最开头添加如下:

import pymysql 
pymysql.install_as_MySQLdb()

[root@zabbix 09:56:13/data/recycle/python/mysitel]# cat mysitel/__init__.py 
import pymysql
pymysql.install_as_MySQLdb()
[root@zabbix 09:56:21/data/recycle/python/mysitel]#

四、修改启动访问服务端的IP地址

 

 五、启动项目

[root@zabbix 10:01:50/data/recycle/python/mysitel]# python3 manage.py runserver 0.0.0.0:800
Performing system checks...

System check identified no issues (0 silenced).

You have 15 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.

February 09, 2022 - 10:02:11
Django version 2.1.10, using settings 'mysitel.settings'
Starting development server at http://0.0.0.0:800/
Quit the server with CONTROL-C.
[09/Feb/2022 10:14:54] "GET /test_port HTTP/1.1" 200 311
Not Found: /favicon.ico
[09/Feb/2022 10:14:54] "GET /favicon.ico HTTP/1.1" 404 3655
Performing system checks...

六、在mysql里创建数据库

mysql> create database mysitel default charset utf8;
Query OK, 1 row affected, 1 warning (0.00 sec)

七、启动mysql模块

 

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysitel',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '172.17.0.100',
        'PORT': '3306',
    }
}

八、 建立应用

[root@zabbix 10:11:28/data/recycle/python/mysitel]# python3 manage.py startapp bookstore

九、注册激活应用,并禁用 防止csrf攻击  该项,不用测试有点麻烦

 十、在应用下的models.py文件中编写模型类,Book类

[root@zabbix 11:33:35/data/recycle/python/mysitel]# cat bookstore/models.py 
from django.db import models


# Create your models here.
class Book(models.Model):
    title = models.CharField("书名", max_length=50)
    price = models.DecimalField("定价", max_digits=5, decimal_places=2)

[root@zabbix 11:33:44/data/recycle/python/mysitel]# 

十一、根据模型类生成迁移文件

[root@zabbix 10:28:52/data/recycle/python/mysitel]# python3 manage.py makemigrations
Migrations for 'bookstore':
  bookstore/migrations/0001_initial.py
    - Create model Book
[root@zabbix 10:29:46/data/recycle/python/mysitel]# 

十二、根据迁移文件同步数据库

[root@zabbix 10:30:48/data/recycle/python/mysitel]# python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, bookstore, contenttypes, sessions
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 admin.0003_logentry_add_action_flag_choices... 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 auth.0009_alter_user_last_name_max_length... OK
  Applying bookstore.0001_initial... OK
  Applying sessions.0001_initial... OK
[root@zabbix 10:31:29/data/recycle/python/mysitel]# 

十三、登录数据库查看表数据

 

十四、生成表之后,在Django shell中,向表添加数据

[root@zabbix 10:50:49/data/recycle/python/mysitel]# python3 manage.py shell
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from bookstore.models import Book
>>> Book.objects.create(title='python',price=50)
<Book: Book object (1)>
>>> Book.objects.create(title='django',price=100)
<Book: Book object (2)>
>>> 

# 上面所执行的命令行 

python3 manage.py shell  # 进入Django中的shell

from bookstore.models import Book

Book.objects.create(title='python',price=50)

Book.objects.create(title='django',price=100)

 十五、在mysql里查看表数据

mysql> select * from bookstore_book;
+----+--------+--------+
| id | title  | price  |
+----+--------+--------+
|  1 | python |  50.00 |
|  2 | django | 100.00 |
+----+--------+--------+
2 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值