当我去localhost:8000/api/create
时,我得到了:
views.py :
from rest_framework import viewsets
from rest_framework.generics import (
ListAPIView,
RetrieveAPIView,
CreateAPIView,
# DestroyAPIView,
# UpdateAPIView
)
from articles.models import Article
from .serializers import ArticleSerializer
class ArticleListView(ListAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
class ArticleDetailView(RetrieveAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
class ArticleCreateView(CreateAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
urls.py :
from django.urls import path, include
from .views import ArticleListView, ArticleDetailView, ArticleCreateView
urlpatterns = [
path('', ArticleListView.as_view()),
path('create/', ArticleCreateView.as_view()),
path('<pk>', ArticleDetailView.as_view()),
]
models.py :
from django.db import models
# Article Model
class Article(models.Model):
title = models.CharField(max_length=120)
content = models.TextField()
def __str__(self):
return self.title
我在Windows 10上,使用python 3.7.1,我不知道还要写些什么,但是我正在写这些字,所以这个东西可以让我发布,而无需理会。 p>