from django.urls import path, re_path, include
from django.conf.urls import url
from . import views
# The following is to use rest framework The routing system is set
from rest_framework import routers
router = routers.DefaultRouter() # Instantiate a default class
router.register(prefix=r'xxx', viewset=views.V3View) # Register in the system , Note the parameters here ,prefix Prefix means , Generated url There will be this ,viewset Which view you want to register , Here is V3View
router.register(prefix=r'rt', viewset=views.V3View) # There's nothing wrong here , Because the prefix is different , Generated url Is different . Register in the system , Note the parameters here ,prefix Prefix means , Generated url There will be this ,viewset Which view you want to register , Here is V3View
# How to verify the above routing settings , And the following settings , As long as we are url Misspelled in , Then it will report many wrong routes , You can see whether there is an automatically generated route url
urlpatterns = [
re_path(r'^roles/$', views.RolesView.as_view()),
re_path(r'^userinfo/$', views.UserInfoView.as_view()),
re_path(r'^group/(?P<pk>\d+)$', views.GroupView.as_view(), name='gp'), # Notice the pk, It is written in the default system , Can be in view To get rid of ., The source code in : class HyperlinkedRelatedField(RelatedField): lookup_field = 'pk' This pk That's what people default on pk, however pk incorrect , because pk yes id,id It is changing. , Two groups are fixed .
re_path(r'^usergroup/$', views.UserGroupView.as_view()),
re_path(r'^page1/$', views.Page1View.as_view()), # This is when testing the paging function URL
re_path(r'^page2/$', views.Page1View_2.as_view()), # This is when testing the paging function URL
re_path(r'^page3/$', views.Page1View_3.as_view()), # This is when testing the paging function URL
re_path(r'^v1/$', views.V1View.as_view()), # This is the test restframework View View function URL
re_path(r'^v2/$', views.V2View.as_view({
'get': 'xxx'})), # here V2View() Inherited GenericViewSet, This class overrides as_view() Method , Delivery required action Method
# here V3View() Inherited ModelViewSet, ModelViewSet And inherited GenericViewSet(), then GenericViewSet Rewrote as_view() Method , So our customized class view inherits ModelViewSet() class
# Also rewrite url Medium as_view(), Pass him parameters .
# First of all, this one-to-one correspondence requires querying the source code , There are specific function names in the source code .
re_path(r'^v3/$', views.V3View.as_view({
'get': 'list', 'post':'create'})), # Pay attention to this url First half of , Didn't write that pk, What do you mean , Express get All the data is requested ,post The request does not need to be specified pk, You can directly pass the corresponding fields to the database ,
# Note the following url, This one's inside pk\d+ It refers to a data in the database , It is no longer a whole , Operate on a data , Look at the corresponding methods . Remember not to make mistakes . You can realize the function of adding, deleting, modifying and checking one by one
re_path(r'^v3/(?P<pk>\d+)/$', views.V3View.as_view({
'get': 'retrieve', 'delete':'destroy', 'put':'update', 'patch':'partial_update'})),
#'''
# The following code is the routing system , Automatically generated routes
# Why should the system automatically generate url, Because for a view , Tend to have 2 individual url, Because it is the reason to obtain the overall and individual data , Then add json Format reason , There will be one
# The view generates 4 individual url, Then if you think it's troublesome to write , You can set some here , Let the system help you generate
#'''
# url(r'(?P<version>)[v1|v2]/$', include(router.urls)), Writing with version prefix .
url(r'^', include(router.urls)), # You have to use it here url(),re_path and path no way .
# Below url It is used to test the renderer
url(r'^test/$', views.TestView.as_view())
]
from django.db import models
class UserInfo(models.Model):
user_type_choices = (
(1, ' Ordinary users '),
(2, 'VIP Customer '),
(3, 'SVIP')
)
user_type = models.IntegerField(choices=user_type_choices)
username = models.CharField(max_length=32, unique=True)
password = models.CharField(max_length=64)
group = models.ForeignKey(to='UserGroup', on_delete=models.CASCADE) # Suppose a group corresponds to multiple people , One person only has multiple groups
roles = models.ManyToManyField('Role')
class UserGroup(models.Model):
title = models.CharField(max_length=32)
class Role(models.Model):
title = models.CharField(max_length=32)
######################## The renderer starts ########################
# Or where role Data table , Page first , Post serialization
class Role_Render_Serializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = '__all__'
# Method 1: use values And then use list Type get out Queryset A column of type , Or a few columns , convert to list type , And then use json, You can output .
from rest_framework.renderers import JSONRenderer, AdminRenderer, HTMLFormRenderer
class TestView(APIView):
# The renderer can write here , You can also write setting.py In the document
renderer_classes = [JSONRenderer, AdminRenderer, HTMLFormRenderer] # Add the class of renderer here , This is the same as the previous permission and Auth equally , All are classes , Put it in the list , Then let the system instantiate itself .
def get(self, request, *args, **kwargs):
# Take out all role
roles = Role.objects.all()
# Set paging parameters
pg = PageNumberPagination()
pg.page_size = 2
pg.max_page_size = 10
pg_ret = pg.paginate_queryset(queryset=roles, request=request, view=self)
# Serialized data
ser = Role_Render_Serializer(instance=pg_ret, many=True)
return Response(ser.data)
Python reports an error when running, but it works fine on other computers, but it doesnt work if you change the computer. What is the reason for this?
Python runs an error, other co
I used Python to crawl wechat friends. It turns out that they are such people
With the popularity of wechat