這個設置沒有問題,從教程中撕掉。
項目文件夾django_example
。
應用文件夾events
。
django_example/settings.py
地址:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'events.apps.EventsConfig'
]
地址:
events/apps.py
from __future__ import unicode_literals
from django.apps import AppConfig
class EventsConfig(AppConfig):
name = 'events'
爲背景,events/models.py
看起來是這樣的:
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Event(models.Model):
name = models.TextField()
kind = models.TextField()
location = models.TextField()
start_hour = models.TextField()
end_hour = models.TextField()
creator = models.TextField()
class Meta:
verbose_name = "Event"
verbose_name_plural = "Events"
def __str__(self):
return self.name
請勿使用type
作爲字段名稱。
地址:
events/serializers.py
from rest_framework import serializers
from .models import Event
class EventSerializer(serializers.ModelSerializer):
class Meta:
model = Event
fields = ('name', 'kind')
events/views.py
地址:
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from events.models import Event
from events.serializers import EventSerializer
class JSONResponse(HttpResponse):
"""
An HttpResponse that renders its content into JSON.
"""
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)
@csrf_exempt
def event_list(request):
"""
List all code snippets, or create a new snippet.
"""
if request.method == 'GET':
snippets = Event.objects.all()
serializer = EventSerializer(snippets, many=True)
return JSONResponse(serializer.data)
elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = EventSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JSONResponse(serializer.data, status=201)
return JSONResponse(serializer.errors, status=400)
@csrf_exempt
def event_detail(request, pk):
"""
Retrieve, update or delete a code snippet.
"""
try:
event = Event.objects.get(pk=pk)
except event.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'GET':
serializer = EventSerializer(snippet)
return JSONResponse(serializer.data)
elif request.method == 'PUT':
data = JSONParser().parse(request)
serializer = EventSerializer(event, data=data)
if serializer.is_valid():
serializer.save()
return JSONResponse(serializer.data)
return JSONResponse(serializer.errors, status=400)
elif request.method == 'DELETE':
event.delete()
return HttpResponse(status=204)
events/urls.py
地址:
from django.conf.urls import url
from events import views
urlpatterns = [
url(r'^events/$', views.event_list),
url(r'^events/(?P<pk>[0-9]+)/$', views.event_detail),
]
地址:
django_example/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('events.urls'))
]
如果你跑你的遷移人口,你以後models.py你應該能夠看到來自API的響應,即使它是隻是一個空的列表。
請添加路線和視圖集。 – seanmus
@SeanM我添加了路線。但我沒有任何視圖集,因爲我認爲,get方法和視圖集一樣。不是嗎? – cano
您是否運行makemigrations並遷移? – seanmus