Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Clark Lin
django101
Commits
23d86193
Commit
23d86193
authored
Jun 04, 2024
by
Administrator
Browse files
added custom permission; added login/logout function
parent
f36e7f0f
Changes
2
Hide whitespace changes
Inline
Side-by-side
django_rest/urls.py
View file @
23d86193
...
@@ -21,5 +21,6 @@ from rest_framework.documentation import include_docs_urls
...
@@ -21,5 +21,6 @@ from rest_framework.documentation import include_docs_urls
urlpatterns
=
[
urlpatterns
=
[
path
(
'admin/'
,
admin
.
site
.
urls
),
path
(
'admin/'
,
admin
.
site
.
urls
),
path
(
'my_app/'
,
include
(
'my_app.urls'
)),
path
(
'my_app/'
,
include
(
'my_app.urls'
)),
path
(
'docs/'
,
include_docs_urls
(
title
=
"DRF API文档"
,
description
=
"Django REST Framework快速入门"
))
path
(
'docs/'
,
include_docs_urls
(
title
=
"DRF API文档"
,
description
=
"Django REST Framework快速入门"
)),
path
(
'api-auth'
,
include
(
'rest_framework.urls'
))
]
]
my_app/views.py
View file @
23d86193
...
@@ -14,12 +14,17 @@ from rest_framework.views import APIView
...
@@ -14,12 +14,17 @@ from rest_framework.views import APIView
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
from
rest_framework
import
status
from
rest_framework
import
status
# Import for custom permission
from
my_app.permissions
import
CustomPermission
"""
"""
CBV - Class based views
CBV - Class based views
"""
"""
# Used to query multiple items or create new item
# Used to query multiple items or create new item
class
DemoUserList
(
APIView
):
class
DemoUserList
(
APIView
):
permission_classes
=
[
CustomPermission
]
# Query all items
# Query all items
def
get
(
self
,
request
,
format
=
None
):
def
get
(
self
,
request
,
format
=
None
):
demo_users
=
DemoUsers
.
objects
.
all
()
demo_users
=
DemoUsers
.
objects
.
all
()
...
@@ -28,6 +33,7 @@ class DemoUserList(APIView):
...
@@ -28,6 +33,7 @@ class DemoUserList(APIView):
# Create new item
# Create new item
def
post
(
self
,
request
,
format
=
None
):
def
post
(
self
,
request
,
format
=
None
):
serializer
=
DemoUsersSerializer
(
data
=
request
.
data
)
serializer
=
DemoUsersSerializer
(
data
=
request
.
data
)
if
serializer
.
is_valid
():
if
serializer
.
is_valid
():
serializer
.
save
(
created_by
=
self
.
request
.
user
.
username
)
serializer
.
save
(
created_by
=
self
.
request
.
user
.
username
)
...
@@ -42,6 +48,8 @@ class DemoUserList(APIView):
...
@@ -42,6 +48,8 @@ class DemoUserList(APIView):
# Used to query/update/delete single item
# Used to query/update/delete single item
class
DemoUserDetail
(
APIView
):
class
DemoUserDetail
(
APIView
):
permission_classes
=
[
IsAuthenticated
]
# Implementation of get single item
# Implementation of get single item
def
get_object
(
self
,
pk
):
def
get_object
(
self
,
pk
):
try
:
try
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment