我正在学习有关 Django Rest Framework 的课程。我似乎已经逐字复制了代码,但是,似乎存在一些我无法归零的错误。什么可能导致此错误?
基本上,我正在测试模型序列化器。我正在尝试发布以下数据。
{
"id": 1,
"author": "John Doe",
"title": "Happy Birthday",
"description": "20 years of ISS",
"body": "Fancy content",
"location": "Earth",
"publication_date": "2020-06-11",
"active": false,
}
Run Code Online (Sandbox Code Playgroud)
我的序列化器类看起来:
class ArticleSerializer(serializers.ModelSerializer):
time_since_publication = serializers.SerializerMethodField()
class Meta:
model = Article
fields = '__all__'
def get_time_since_publication(self, object):
publication_date = object.publication_date
now = datetime.now()
time_delta = timesince(publication_date, now)
return time_delta
Run Code Online (Sandbox Code Playgroud)
我的模型是:
class Article(models.Model):
author = models.CharField(max_length=50)
title = models.CharField(max_length=120)
description = models.CharField(max_length=200)
body = models.TextField()
location = models.CharField(max_length=120)
publication_date = models.DateField() …Run Code Online (Sandbox Code Playgroud) django django-models django-views django-serializer django-rest-framework
我正在尝试遵循 Python TDD O'Reilly。
我知道他们使用的 django 版本是旧版本,但是,即使在进行了所有可能的修改之后,我仍然收到此错误。
我的 urls.py 文件:
超级列表.urls.py
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('lists.urls')),
]
Run Code Online (Sandbox Code Playgroud)
列表.urls.py
urlpatterns = [
path('/', home_page)
]
Run Code Online (Sandbox Code Playgroud)
列表.views.py
from django.shortcuts import (render, HttpResponse)
from rest_framework.decorators import api_view
# Create your views here.
@api_view()
def home_page(request):
return HttpResponse('Response')
Run Code Online (Sandbox Code Playgroud)
列表.tests.py
from django.test import TestCase
from .views import home_page #
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
found = reverse('/')
self.assertEqual(found.func, home_page)
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我尝试搜索这个问题很长时间,但没有成功。