我执行runserver并尝试访问API时收到此错误:
FileNotFoundError: [WinError 3] The system cannot find the path specified: ''
我检查导入,模型,序列化器,视图集和URL。我不知道这是什么意思?
这是错误的屏幕截图:
我进入以下网址时遇到了异常情况:http://127.0.0.1:8000/api/v1.0/notes/
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/api/v1.0/notes/
Django Version: 2.2.4
Python Version: 3.7.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'note',
'rest_framework']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
The traceback is:
Traceback:
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\django\core\handlers\base.py" in _get_response
145. response = self.process_exception_by_middleware(e, request)
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\django\core\handlers\base.py" in _get_response
143. response = response.render()
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\django\template\response.py" in render
106. self.content = self.rendered_content
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\rest_framework\response.py" in rendered_content
70. ret = renderer.render(self.data, accepted_media_type, context)
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\rest_framework\renderers.py" in render
725. context = self.get_context(data, accepted_media_type, renderer_context)
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\rest_framework\renderers.py" in get_context
656. raw_data_post_form = self.get_raw_data_form(data, view, 'POST', request)
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\rest_framework\renderers.py" in get_raw_data_form
564. data = serializer.data.copy()
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\rest_framework\serializers.py" in data
559. ret = super().data
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\rest_framework\serializers.py" in data
265. self._data = self.get_initial()
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\rest_framework\serializers.py" in get_initial
409. for field in self.fields.values()
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\django\utils\functional.py" in __get__
80. res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\rest_framework\serializers.py" in fields
360. for key, value in self.get_fields().items():
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\rest_framework\serializers.py" in get_fields
1056. fields[field_name] = field_class(**field_kwargs)
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\rest_framework\fields.py" in __init__
1503. allow_folders=allow_folders, required=required
File "C:\Users\lrond\Desktop\projectEnreda\env\lib\site-packages\django\forms\fields.py" in __init__
1109. for f in os.scandir(self.path):
Exception Type: FileNotFoundError at /api/v1.0/notes/
Exception Value: [WinError 3] El sistema no puede encontrar la ruta especificada: ''
serializer.py
from .models import Note
from rest_framework import serializers
class NoteSerializer(serializers.ModelSerializer):
class Meta:
model = Note
fields = '__all__'
viewsets.py
from rest_framework import viewsets
from .models import Note
from .serializer import NoteSerializer
class NoteViewSet(viewsets.ModelViewSet):
queryset = Note.objects.all()
serializer_class = NoteSerializer
urls.py
from rest_framework import routers
from .viewsets import NoteViewSet
router = routers.SimpleRouter()
router.register('notes', NoteViewSet)
urlpatterns = router.urls
答案 0 :(得分:1)
问题出在models.py中,我有一个FilePathField。
感谢@IşıkKaplan
from django.db import models
# Create your models here.
class Note(models.Model):
date = models.DateTimeField()
endDate = models.DateTimeField()
note = models.TextField()
attachment = models.CharField(max_length=50)
task = models.BooleanField()
tag = models.CharField(max_length=50)
type = models.CharField(max_length=50)