我正在关注 wsvincent django rest framewok教程。我已经完成了所有在那里写的工作,但是当我运行localhost / snippets / 1 /时,它会引发以下异常
摘录中的TypeError / 1 /
init ()接受1个位置参数,但给出了2个。
如果您知道出了什么问题,请提供帮助。
localhost / snippets运行正常。这是回溯。
models.py
from django.db import models
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles
# Create your models here.
LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted((item, item) for item in get_all_styles())
class Snippet(models.Model):
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, blank=True, default='')
code = models.TextField()
linenos = models.BooleanField(default=False)
language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
class Meta:
ordering = ('created',)
def __str__(self):
return self.title
serializers.py
from rest_framework import serializers
from .models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
class SnippetSerializer(serializers.ModelSerializer):
class Meta:
model = Snippet
fields=('id','title','code','linenos','language','style',)
views.py
from django.shortcuts import render
from rest_framework import generics
from .serializers import SnippetSerializer
from .models import Snippet
# Create your views here.
class SnippetSerializer(generics.ListCreateAPIView):
queryset = Snippet.objects.all()
serializer_class=SnippetSerializer
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Snippet.objects.all()
serializer_class=SnippetSerializer
urls.py
from django.urls import path,include
from rest_framework.urlpatterns import format_suffix_patterns
from . import views
urlpatterns=[
path('snippets/',views.SnippetSerializer.as_view()),
path('snippets/<int:pk>/',views.SnippetDetail.as_view()),
]
urlpatterns = format_suffix_patterns(urlpatterns)