Django + restframework remember first online book project

Learning materials:

    1.django model field in a document: https://docs.djangoproject.com/...

    2. The difference in ntext SQL text and type in: http://www.uzzf.com/news/2210.html

 

First, set up environment

    Installation virtualenvwrapper: https://www.jianshu.com/p/7ed2dfa86e90

    Create a virtual environment: mkvirtualenv --python = python.exe environment path name (net_book)

    Enter the virtual environment: workon environment name (net_book)

    Installation dependencies: the install Django PIP == 2.2 -i https://pypi.douban.com/simple/

                     pip install pymysql # MySQL database connection

                     pip install django-redis # database connection Redis

                     pip install djangorestframework # RESTFual规则

                     pip install pillow # Image processing package

 

Second, edit project

1, create a project: django-admin startproject project name (net_book) (items stored in the folder)

2, create an application: python manage.py startapp application name (Book) (run the project folder)

3, set the settings:

          Add 'rest_framework' in the INSTALLED_APPS, 'Book',

          LANGUAGE_CODE = ‘zh-hans’

          USE_ZONM = 'Asia / Shanghai'

          USE_TZ = False

4, to create a model: PyCharm connecting Mysql failure [08001] Could not create c. . . .

        Ebook model:

  1 from django.db import models
  2 from Books.constants import *
  3 
  4 class Ebook(models.Model):
  5     name = models.CharField(max_length=32)
  6     author = models.CharField(max_length=32)
  7     introduction = models.TextField()
  8     type = models.IntegerField(default=UNCLASSIFIED)
  9     status = models.IntegerField(default=SERIAL)
 10     price = models.DecimalField(max_digits=6, decimal_places=2, default='0')
 11     thumbnail = models.ImageField(upload_to=f'media/uploads/thumbnail/%Y/%m')
 12     bookcase_num = models.IntegerField(default=0)
 13     vote_num = models.IntegerField(default=0)
 14     weight = models.IntegerField(default=WEIGHT_DEFAULT)
 15     class Meta():
 16         db_table = 'book'
Ebook model:

        Content model:

  1 class Content(models.Model):
  2     suffix1 = models.TextField()
  3     suffix2 = models.TextField()
  4     suffix3 = models.TextField()
  5     suffix4 = models.TextField()
  6     suffix5 = models.TextField()
  7     suffix6 = models.TextField()
  8     suffix7 = models.TextField()
  9     suffix8 = models.TextField()
 10     suffix9 = models.TextField()
 11     suffix0 = models.TextField()
 12     class Meta():
 13         db_table = 'content'
Content model:

    Settings settings:

  1 DATABASES = {
  2     'default': {
  3         # 'ENGINE': 'django.db.backends.sqlite3',
  4         # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  5         'ENGINE': 'django.db.backends.mysql',
  6         'NAME': 'net_book',
  7         'USER': 'root',
  8         'PASSWORD': 'your password',
  9         'HOST': '127.0.0.1',
 10         'POST': '3306',
 11     }
 12 }
Set settings-> DATABASES:

    Generate migrate files: Python manage.py makemigrations

    Migrating data: pyhton manage.py migrate

5, create a serializer:

    Books in the creation serialization.py

Guess you like

Origin www.cnblogs.com/yulincoco/p/11944500.html