DRF preparation DRF preparation D R & lt F. Quasi prepared
1. Installation of related libraries
pip install django -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip install djangorestframework -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
# 跨域
pip install django-cors-headers -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip install pymysql -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
2. Create a django project
django-admin startproject test_django_project
3. Use pycharm to open the test_django_project project
4. Complete some basic configuration
4.1 Database configuration
DATABASES = {
'default': {
# 数据库引擎(是mysql还是oracle等)
'ENGINE': 'django.db.backends.mysql',
# 数据库的名字
'NAME': 'DjangoDB',
# 连接mysql数据库的用户名
'USER': 'root',
# 连接mysql数据库的密码
'PASSWORD': '123456',
# mysql数据库的主机地址
'HOST': '127.0.0.1',
# mysql数据库的端口号
'PORT': '3306',
}
}
4.2 Time zone
TIME_ZONE = 'Asia/Shanghai'
4.3 Programming language
LANGUAGE_CODE = 'zh-CN'
4.4 Install rest_framework
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework'
]
5. DRF project infrastructure
6. Create app and model:
import pymysql
pymysql.install_as_MySQLdb()
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'apps.webauth',
]
Create the model:
from django.db import models
# Create your models here.
class Person(models.Model):
name = models.CharField(max_length=100,null=False)
address = models.CharField(max_length=100,null=False)
7. Add test data:
After creating the model, run makemigrations
and migrate
map the model to the mysql database.
8. Write Serializers:
Create a new file serializers.py in the webauth app, and add the following code:
from rest_framework import serializers
from .models import Person
class PersonSerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields= "__all__"
# exclude = ['','',]
9. Write the view:
Using drf, we can easily create views that contain methods such as get/post. Add the following code in meituan.views:
from rest_framework import viewsets
from .models import Person
from .serializers import PersonSerializer
# 这个视图函数已经包含了增删改查操作
class PersonViewset(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
10. Write routing:
Add the following code in meituan.urls:
from rest_framework.routers import DefaultRouter
from .views import PersonViewset
router = DefaultRouter()
router.register('person',PersonViewset,basename='person')
app_name = 'drf_demo'
urlpatterns = []+router.urls
Then add the route of meituan in the project's urls.py:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('drf_demo/', include('apps.drf_demo.urls')),
]
In the future, we can use different methods to send requests to /meituan/merchant. For example, if you use get, it will return the list of merchants. For example, if you use post, you will add data to the merchant table.
django.db.utils.OperationalError: (1045, “Access denied for user ‘root’@‘117.136.67.229’ (using password: YES)”)
ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
FLUSH PRIVILEGES;