【发布时间】:2018-02-03 20:25:17
【问题描述】:
我正在使用 django 2.0.2 开发一个简单的网络应用程序。我不确定我的代码有什么问题。我收到了错误
ModuleNotFoundError: No module named 'myapp.views.hello'; 'myapp.views' is not a package
这是我的代码
views.py
from django.shortcuts import render
def hello(request):
return render(request,'myapp/templates/hello.html',{})
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include('myapp.views.hello'), name='hello'),
]
即使我做了from myapp.views import *,也会抛出同样的错误
我的代码有什么问题?
这是树形结构
DjProject/
├── db.sqlite3
├── DjProject
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── settings.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── wsgi.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── myapp
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│ ├── __init__.py
│ └── __pycache__
├── models.py
├── __pycache__
│ ├── admin.cpython-36.pyc
│ ├── __init__.cpython-36.pyc
│ ├── models.cpython-36.pyc
│ └── views.cpython-36.pyc
├── templates
│ └── hello.html
├── tests.py
└── views.py
7 directories, 22 files
【问题讨论】:
-
include不是这样工作的......只需import views并使用views.hello。 -
请把你项目的树结构贴在这里。
-
@internet_user 我按照你说的尝试了。这是输出。
File "/home/sukumar/PycharmProjects/DjProject/DjProject/urls.py", line 23, in <module> path('hello/', 'views.hello', name='hello'), File "/usr/local/lib/python3.6/dist-packages/django/urls/conf.py", line 73, in _path raise TypeError('view must be a callable or a list/tuple in the case of include().') TypeError: view must be a callable or a list/tuple in the case of include().
标签: python django python-3.x model-view-controller django-views