Marek Patyna
Marek Patyna

Reputation: 87

Python, Django can't run my server

I try create RESTful API with Django. Can you help me with this problem i don't know why i have this error.

I did migration so i used

python manage.py makemigrations

and

python manage.py migrate

and I created superuser

python manage.py createsuperuser

If do you need it more code, answer. Thanks

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'

MODELS

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

SERIALIZERS

from rest_framework import serializers
from rest_framework import employees

class employeesSerializer(serializers.ModelSerializer):

    class Meta:
        model= employees
        #fields=('firstname','lastname')
        fields= '__all__'

VIEWS

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()),
]

Upvotes: 1

Views: 646

Answers (1)

anmaxvl
anmaxvl

Reputation: 181

Your import is wrong. This import from rest_framework import employees in serializers.py. It should be

from .models import employees

Upvotes: 2

Related Questions