Install and use djangorestframework
This commit is contained in:
0
apiv1/__init__.py
Normal file
0
apiv1/__init__.py
Normal file
3
apiv1/admin.py
Normal file
3
apiv1/admin.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
5
apiv1/apps.py
Normal file
5
apiv1/apps.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class Apiv1Config(AppConfig):
|
||||||
|
name = 'apiv1'
|
||||||
0
apiv1/migrations/__init__.py
Normal file
0
apiv1/migrations/__init__.py
Normal file
7
apiv1/models.py
Normal file
7
apiv1/models.py
Normal 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
3
apiv1/tests.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
13
apiv1/urls.py
Normal file
13
apiv1/urls.py
Normal 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
8
apiv1/views.py
Normal 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
|
||||||
@@ -32,12 +32,14 @@ ALLOWED_HOSTS = []
|
|||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
'bookmarks.apps.BookmarksConfig',
|
'bookmarks.apps.BookmarksConfig',
|
||||||
|
'apiv1.apps.Apiv1Config',
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'rest_framework',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@@ -121,3 +123,11 @@ USE_TZ = True
|
|||||||
# https://docs.djangoproject.com/en/3.1/howto/static-files/
|
# https://docs.djangoproject.com/en/3.1/howto/static-files/
|
||||||
|
|
||||||
STATIC_URL = '/static/'
|
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'
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -22,5 +22,6 @@ urlpatterns = [
|
|||||||
path('', views.home, name='home'),
|
path('', views.home, name='home'),
|
||||||
path('bookmarks/', include('bookmarks.urls')),
|
path('bookmarks/', include('bookmarks.urls')),
|
||||||
path('admin/', admin.site.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')),
|
||||||
]
|
]
|
||||||
|
|||||||
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
Reference in New Issue
Block a user