Translation Table
English | Chinese |
---|---|
front-end | 前端 |
back-end | 后端 |
pagination | 分页 |
tweaking | 拧(这里应该是修改的意思) |
queries | 查询 |
Decimal | 十进制 |
querysets | 查询集 |
Building a Modern Web Application with Django REST Framework and Vue: Building Views and REST API
Throughout this part of these tutorial series you will continue developing a CRUD (Create, Read, Update and Delete) application with a restful API back-end and a Vue front-end using Django, Django REST framework and Vue (with Axios as an HTTP client). In this part you’ll specifically build the REST API and the front-end views to consume and display data from the API endpoints. You will also see how to integrate your Vue application with your Django back-end in production. As always you can find the source code of the demo project in this Github repository.
You can check the second article from this link
- Building the REST API: You will create a simple REST API around one model (Product) with DRF(Django REST Framework) and learn how to add pagination to your APIs.
- Creating the Service to Consume the API: You will create the class that interfaces with your API using Axios.
- Creating the Front End Views: You will create different views and routes for the Vue application and see how you can protect some routes from non authenticated users.
- Getting Ready for Production: Finally you’ll prepare your app for production by tweaking some settings in Vue and Django parts.
Building the REST API
Django REST framework is a powerful and easy to use package for building Web APIs.
Let’s get started by building a simple REST API using Django REST framework.
Adding the Product Model
Django has a powerful ORM (Object Relational Mapper) that allows you to work with multiple database management systems without actually writing any SQL. All you need to do is to define models in Python classes and Django will take care of mapping Python classes to SQL queries.
The API is built around a simple product model so continuing with the project you’ve built in the previous part open the catalog/models.py
file then add the following model
1 | from django.db import models |
This is a Python class that extends the special Django class Model which is imported from the django.db.models
built-in package. Every Django model needs to extends the Modelclass. You then specify the model fields using classes like CharField, TextField and DecimalField etc.
Now you need to migrate your database to add the new changes
1 | python manage.py makemigrations |
Next let’s add some seed data using a data migration
So first make an empty migration by running the following command:
1 | python manage.py makemigrations catalog --empty |
Next open your migration file in your app migrations folder (catalog/migrations
) then create a function that will executed by the RunPython()
method when you apply your migration
1 | from django.db import migrations |
You can then migrate your database to create the initial data
1 | python manage.py migrate |
Adding the Serializer Class
From Django REST framework docs here is the definition of a serializer
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
Serializers允许将复杂数据(如查询集和模型实例)转换为Python数据类型,然后可以轻松地将其呈现为JSON,XML或其他内容类型。 序列化程序还提供反序列化,允许在首次验证传入数据后将解析后的数据转换回复杂类型。
Create a serializers.py
file inside your the catalog app folder then add the following code to create a serializer class for the product model
1 | from rest_framework import serializers |
Adding the API Views
After adding the database model and the serializer class and also some seed data the next thing is to create the API views that will be responsible for creating, updating, deleting and fetching data from the database and send it back to users as JSON database when users request the appropriate API endpoint so go ahead and open the catalog/views.py
file then start by adding the following imports
1 | from rest_framework import status |
This code imports different classes from DRF package, paginator classes to add pagination and then the Product model and its serializer class.
Adding the Product List/Create API View
In your catalog/views.py
add the following view function which can process either GET or POST requests to either return paginated list of products or create a product.
1 |
|
This function first checks if it’s a GET or POST request then preforms the processing based on the type of the request:
- If it’s a GET request, the function retrieves the page number from the GET request or use the first page by default if no page is submitted then retrieves the request page of products, serialize it and return it back alongside with other information such as the next page and previous page links.
- If it’s a POST request, the function creates the product based on the POST data.
Adding the Product Detail API View
Now you need to add the view function that will be responsible for getting, updating or deleting a single product by id depending on the type of the HTTP request (GET, PUT or DELETE).
1 |
|