我尝试使用Django创建RESTful API。你能帮我解决这个问题,我不知道为什么会有这个错误。
我做了迁移所以我用了
python manage.py makemigrations
和
python manage.py migrate
我创建了超级用户
python manage.py createsuperuser
如果您需要更多代码,请回答。感谢
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x05F4C9C0>
Traceback (most recent call last):
File "D:\python\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "D:\python\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "D:\python\lib\site-packages\django\core\management\base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "D:\python\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "D:\python\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "D:\python\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "D:\python\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "D:\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\python\lib\site-packages\django\urls\resolvers.py", line 536, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\python\lib\site-packages\django\urls\resolvers.py", line 529, in urlconf_module
return import_module(self.urlconf_name)
File "D:\python\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "D:\rest-api\django rest\myproject\myproject\urls.py", line 19, in <module>
from webapp import views
File "D:\rest-api\django rest\myproject\webapp\views.py", line 9, in <module>
from . serializers import employeesSerializer
File "D:\rest-api\django rest\myproject\webapp\serializers.py", line 2, in <module>
from rest_framework import employees
ImportError: cannot import name 'employees'
模型
from django.db import models
class employees(models.Model):
firstname=models.CharField(max_length=10)
lastname=models.CharField(max_length=10)
emp_id=models.IntegerField()
def __str__(self):
return self.firstname
串行
from rest_framework import serializers
from rest_framework import employees
class employeesSerializer(serializers.ModelSerializer):
class Meta:
model= employees
#fields=('firstname','lastname')
fields= '__all__'
视图
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from . models import employees
from . serializers import employeesSerializer
class employeeList(APIView):
def get(self, request):
employees1= employees.objects.all()
serializer=employeesSerializer(employees1, many=True)
return Response(serializer.data)
def post(self):
pass
URLS
from django.contrib import admin
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from webapp import views
urlpatterns = [
path('admin/', admin.site.urls),
url('employess/',views.employeeList.as_view()),
]
答案 0 :(得分:2)
导入错误。 from rest_framework import employees
中的此导入serializers.py
。它应该是
from .models import employees