从anshu.myapp.views导入TaskViewSet ModuleNotFoundError:没有名为“ anshu.myapp”的模块

时间:2018-06-19 12:18:31

标签: python django django-rest-framework

我在Django中使用rest框架。我的工作目录和项目如下所示。

运行服务器时出现错误消息

ModuleNotFoundError: No module named 'anshu.myapp'

请帮助我。

anshu
    anshu
        __init__.py
        settings.py
        urls.py
        wsgi.py
    myapp
        migrations
        __init__.py
        admin.py
        models.py
        Serializers.py
        tests.py
        urls.py
        views.py
    manage.py

这是我的设置文件。

settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'rest_framework',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from rest_framework import routers
from .views import TaskViewSet


router = routers.DefaultRouter()
router.register(r'task',TaskViewSet)

urlpatterns = [

    url(r'^',include(router.urls)),
    url(r'^admin/', include(admin.site.urls)),
]

views.py

from .models import Task
from rest_framework import viewsets
from .Serializers import TaskSerializer


class TaskViewSet(viewsets.ModelViewSet):

    queryset = Task.objects.all().order_by('-date_created')
    serializer_class = TaskSerializer

model.py

from django.db import models

class Task(models.Model):
    task_name=models.CharField(max_length=20)
    task_desc=models.TextField(max_length=200)
    date_created=models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.task_name

“ anshu.myapp.views import TaskViewSet”的urls文件中是无法运行服务器的问题

我正在使用:-

python = 3
django = 2.0.3
rest_framework = 3.8.2

追踪

(py1) C:\Users\NIKITA GULHANE\nehal\anshu>python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function check_errors. 
<locals>.wrapper at 0x0000019A3FBB8B70>
Traceback (most recent call last):
File "C:\Users\NIKITA~1\Envs\py1\lib\site- 
packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Users\NIKITA~1\Envs\py1\lib\site- 
packages\django\core\management\commands\runserver.py", line 120, in 
inner_run
self.check(display_num_errors=True)
File "C:\Users\NIKITA~1\Envs\py1\lib\site- 
packages\django\core\management\base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\NIKITA~1\Envs\py1\lib\site- 
packages\django\core\management\base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\NIKITA~1\Envs\py1\lib\site- 
packages\django\core\checks\registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\NIKITA~1\Envs\py1\lib\site- 
packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\NIKITA~1\Envs\py1\lib\site- 
packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\NIKITA~1\Envs\py1\lib\site-packages\django\urls\resolvers.py", 
line 397, in check
for pattern in self.url_patterns:
File "C:\Users\NIKITA~1\Envs\py1\lib\site- 
packages\django\utils\functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\NIKITA~1\Envs\py1\lib\site-packages\django\urls\resolvers.py", 
line 536, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\NIKITA~1\Envs\py1\lib\site- 
packages\django\utils\functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\NIKITA~1\Envs\py1\lib\site-packages\django\urls\resolvers.py", 
line 529, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\NIKITA~1\Envs\py1\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 "C:\Users\NIKITA GULHANE\nehal\anshu\anshu\urls.py", line 4, in <module>
from .views import TaskViewSet
ModuleNotFoundError: No module named 'anshu.views'

2 个答案:

答案 0 :(得分:3)

您的外部anshu目录位于Python路径上,因此在导入anshu时不应包含myapp前缀。更改为:

from myapp.views import TaskViewSet

答案 1 :(得分:1)

您可以按照@Alasdair的回答将其更改为以下代码

from myapp.views import TaskViewSet

from .views import TaskViewSet

另一个问题是views.py

它是viewsets.ModelViewSet,而不是viewsets.Model.ViewSet

class TaskViewSet(viewsets.ModelViewSet):