Django rest framework修改API页面默认标题

在python的安装目录下,按以下路径查找:
Python36\Lib\site-packages\rest_framework\templates\rest_framework
rest framework默认页面模板路径
比如这次需要修改的是API页面的标题,打开api.html

{
   % extends "rest_framework/base.html" %}

{
   # Override this template in your own templates directory to customize #}

可以看到api.html还是参照base.html的设置,所以去base.html去修改:

            <span>
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Django REST framework是一个基于Django的强大Web API框架,可以帮助开发者快速构建RESTful API。下面是Django REST framework后端开发的文档。 ## 安装 可以通过pip安装Django REST framework: ``` pip install djangorestframework ``` ## 配置 1. 将`rest_framework`添加到`INSTALLED_APPS`中: ```python INSTALLED_APPS = [ ... 'rest_framework', ... ] ``` 2. 添加REST framework默认设置到`settings.py`文件中: ```python REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.AllowAny', ], 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', ], } ``` ## 序列化 Django REST framework的核心是序列化。它允许将复杂的Python数据结构转换为JSON、XML等格式。 ### 创建序列化器 1. 创建一个`serializers.py`文件,并导入`serializers`: ```python from rest_framework import serializers from .models import Book ``` 2. 创建一个`BookSerializer`类: ```python class BookSerializer(serializers.ModelSerializer): class Meta: model = Book fields = '__all__' ``` `fields`属性指定了需要序列化的模型字段。 ### 使用序列化器 在视图中使用序列化器,可以将模型转换为JSON格式的响应。 ```python from rest_framework import generics from .models import Book from .serializers import BookSerializer class BookList(generics.ListCreateAPIView): queryset = Book.objects.all() serializer_class = BookSerializer ``` ## 视图 Django REST framework提供了多种视图,包括基于函数的视图和基于类的视图。 ### 基于函数的视图 1. 创建一个`views.py`文件,并导入Django REST framework的视图: ```python from rest_framework.decorators import api_view from rest_framework.response import Response ``` 2. 创建一个`book_list`函数: ```python @api_view(['GET', 'POST']) def book_list(request): if request.method == 'GET': books = Book.objects.all() serializer = BookSerializer(books, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = BookSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400) ``` `@api_view`装饰器表示该视图只接受`GET`和`POST`请求。`BookSerializer`用于将模型转换为JSON格式的响应。 ### 基于类的视图 1. 创建一个`views.py`文件,并导入Django REST framework的视图: ```python from rest_framework import generics ``` 2. 创建一个`BookList`类: ```python class BookList(generics.ListCreateAPIView): queryset = Book.objects.all() serializer_class = BookSerializer ``` `queryset`属性指定需要序列化的模型,`serializer_class`属性指定使用的序列化器。 ## 路由 Django REST framework的路由是基于Django的URLconf的。可以使用Django REST framework自带的`DefaultRouter`或者自定义路由。 ### 使用`DefaultRouter` 1. 创建一个`urls.py`文件,并导入`DefaultRouter`: ```python from rest_framework.routers import DefaultRouter ``` 2. 在`views.py`中导入视图,并创建一个`router`: ```python from .views import BookList router = DefaultRouter() router.register(r'books', BookList) ``` `r'books'`是路由名称,`BookList`是视图类。 3. 在`urls.py`中将路由添加到URLconf中: ```python from django.urls import path, include urlpatterns = [ path('', include(router.urls)), ] ``` ### 自定义路由 1. 创建一个`urls.py`文件,并导入视图: ```python from django.urls import path from .views import book_list ``` 2. 创建一个URLconf: ```python urlpatterns = [ path('books/', book_list, name='book-list'), ] ``` `book_list`是一个基于函数的视图。 3. 在主URLconf中包含自定义路由: ```python from django.urls import path, include urlpatterns = [ path('', include('myapp.urls')), ] ``` ## 认证和权限 Django REST framework提供了多种认证和权限选项,可以通过`settings.py`中的`DEFAULT_AUTHENTICATION_CLASSES`和`DEFAULT_PERMISSION_CLASSES`设置默认选项。 ### 认证 Django REST framework提供了多种认证选项,包括SessionAuthentication、BasicAuthentication和TokenAuthentication等。 ```python REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', 'rest_framework.authentication.TokenAuthentication', ], } ``` ### 权限 Django REST framework提供了多种权限选项,包括AllowAny、IsAuthenticated和IsAdminUser等。 ```python REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.AllowAny', 'rest_framework.permissions.IsAuthenticated', 'rest_framework.permissions.IsAdminUser', ], } ``` ## 参考资料 - [Django REST framework官方文档](https://www.django-rest-framework.org/) - [Django REST framework教程](https://www.django-rest-framework.org/tutorial/1-serialization/)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值