Install and use djangorestframework

This commit is contained in:
2021-04-01 20:36:29 -05:00
parent 567226684d
commit da85e74860
11 changed files with 51 additions and 1 deletions

0
apiv1/__init__.py Normal file
View File

3
apiv1/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
apiv1/apps.py Normal file
View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class Apiv1Config(AppConfig):
name = 'apiv1'

View File

7
apiv1/models.py Normal file
View File

@@ -0,0 +1,7 @@
from rest_framework import serializers
from bookmarks.models import Bookmark
class BookmarkSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Bookmark
fields = ['title', 'url']

3
apiv1/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

13
apiv1/urls.py Normal file
View File

@@ -0,0 +1,13 @@
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'bookmarks', views.BookmarkViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

8
apiv1/views.py Normal file
View File

@@ -0,0 +1,8 @@
from rest_framework import viewsets
from . import models
from bookmarks.models import Bookmark
class BookmarkViewSet(viewsets.ModelViewSet):
queryset = Bookmark.objects.all()
serializer_class = models.BookmarkSerializer

View File

@@ -32,12 +32,14 @@ ALLOWED_HOSTS = []
INSTALLED_APPS = [
'bookmarks.apps.BookmarksConfig',
'apiv1.apps.Apiv1Config',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]
MIDDLEWARE = [
@@ -121,3 +123,11 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}

View File

@@ -22,5 +22,6 @@ urlpatterns = [
path('', views.home, name='home'),
path('bookmarks/', include('bookmarks.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls'))
path('accounts/', include('django.contrib.auth.urls')),
path('api/v1/', include('apiv1.urls')),
]

Binary file not shown.