当前位置:网站首页>DRF use: get request to get data (small example)
DRF use: get request to get data (small example)
2022-07-27 16:19:00 【fresh_ nam】
List of articles
Preface
The previous blogs talked about drf Some basic knowledge , Don't understand, drf You can go in and study DRF Learning notes ( Get ready )( I will ignore this sentence ), Next, take a simple example to show how the page drf Data transmitted .
One 、 Get ready
So let's create one django project , And add a app, I added app yes demo( Don't forget it. settings Created by reference in app). Then add the following code ( The following is my model class , You can also modify the code according to your own model class ):
demo/models.py
from django.db import models
# Create your models here.
class ClassInfo(models.Model):
number = models.IntegerField(verbose_name=' Class number ')
grade = models.CharField(verbose_name=' grade ', max_length=3, choices=((' In grade one ', ' In grade one '), (' second grade ', ' second grade '), (' Third grade ', ' Third grade ')))
demo/serializers.py
from rest_framework import serializers
from demo.models import ClassInfo
class ClassInfoSerializer(serializers.ModelSerializer):
""" Class data serializer """
class Meta:
model = ClassInfo
fields = '__all__'
demo/views.py
from demo.models import ClassInfo
from demo.serializers import ClassInfoSerializer
from rest_framework import mixins
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import action
class ClassInfoViewSet(mixins.ListModelMixin, GenericViewSet):
queryset = ClassInfo.objects.all()
serializer_class = ClassInfoSerializer
demo/urls.py
from django.urls import path
from demo import views
urlpatterns = [
path('classes_set/', views.ClassInfoViewSet.as_view({
'get': 'list'})),
]
The root of the project urls.py
urlpatterns = [
path('demo/', include('demo.urls')),
]
result :
Two 、 Use
stay templates Add under directory html:
templates/drf_test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>drf_test</title>
<script type="text/javascript" src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js" charset="utf-8"></script>
</head>
<body>
<table border="1" id="class_data">
<tr>
<th> Class number </th>
<th> grade </th>
</tr>
</table>
</body>
<script> var get_url = 'http://127.0.0.1:8000/demo/classes_set/' $.ajax( {
type: 'get', url: get_url, dataType: 'json', success: function (data) {
for(i = 0; i < data.length; i++){
$("#class_data").append('<tr><th>' + data[i].number + '</th>' + '<th>' + data[i].grade + '</th></tr>') } } } ) </script>
</html>
Add view :
demo/views.py
def drf_test(request):
return render(request, 'drf_test.html')
add to url:
demo/urls.py
urlpatterns = [
path('drf_test/', views.drf_test), # Added this line
]
result :
The page can successfully obtain data , And show it !
边栏推荐
- Leetcode 226 flip binary tree (recursive)
- Paper_Book
- 编码技巧——全局异常捕获&统一的返回体&业务异常
- centos上mysql5.7主从热备设置
- Excel extract duplicates
- Mapreduce实例(一):WordCount
- 企业运维安全就用行云管家堡垒机!
- Firefox old version
- Solve the compilation warning of multiple folders with duplicate names under the openwrt package directory (call subdir function)
- ARIMA model selection and residuals
猜你喜欢

Determine the exact type of data

Mapreduce实例(二):求平均值

For enterprise operation and maintenance security, use the cloud housekeeper fortress machine!

openwrt 增加RTC(MCP7940 I2C总线)驱动详解

MapReduce instance (I): wordcount

centos上mysql5.7主从热备设置

Common tool classes under JUC package

判断数据的精确类型

Penetration test - dry goods | 80 + network security interview experience post (interview)

Flask连接mysql数据库已有表
随机推荐
编码技巧——全局异常捕获&统一的返回体&业务异常
Pycharm导入已有的本地安装包
keil 采用 makefile 实现编译
Content ambiguity occurs when using transform:translate()
Addition, deletion, query and modification of MySQL table data
Install MySQL using CentOS yum
2.2 JMeter基本元件
使用transform:translate()出现内容模糊问题
Determine the exact type of data
无线网络分析有关的安全软件(aircrack-ng)
单机高并发模型设计
QT (VI) value and string conversion
First acquaintance with MySQL database
43亿欧元现金收购欧司朗宣告失败!ams表示将继续收购
Ncnn reasoning framework installation; Onnx to ncnn
Hematemesis finishing c some commonly used help classes
Paper_Book
Chapter I Marxist philosophy is a scientific world outlook and methodology
mysql设置密码时报错 Your password does not satisfy the current policy requirements(修改·mysql密码策略设置简单密码)
一款功能强大的Web漏洞扫描和验证工具(Vulmap)