我正在尝试使用Django 2.0.2创建一个简单的webapp。但是我得到了ImportError。搜索了很多,但没有找到解决方法。有人可以帮我解决这个错误。模块“myapp.templates”没有定义“hello”属性/类。
这是我的代码
views.py
from django.shortcuts import render
def hello(request):
return render(request,'hello.html',{})
urls.py
from django.contrib import admin
from django.urls import path
from myapp.views import hello as myapp_hello
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', myapp_hello),
]
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'x+ve9z=f1_%t@*4uo@!h#b*!g-1ayc1d4+ct$q853nczss)#5c'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]
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',
]
ROOT_URLCONF = 'DjProject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'myapp.templates.hello',
],
},
},
]
WSGI_APPLICATION = 'DjProject.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
文件夹结构
.
├── 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__
│ └── __init__.cpython-36.pyc
├── models.py
├── __pycache__
│ ├── admin.cpython-36.pyc
│ ├── __init__.cpython-36.pyc
│ ├── models.cpython-36.pyc
│ └── views.cpython-36.pyc
├── templates
│ ├── hello.html
│ └── __pycache__
├── tests.py
└── views.py
8 directories, 23 files
我收到的错误:
ImportError at /hello/
Module "myapp.templates" does not define a "hello" attribute/class
Request Method: GET
Request URL: http://127.0.0.1:8000/hello/
Django Version: 2.0.2
Exception Type: ImportError
Exception Value:
Module "myapp.templates" does not define a "hello" attribute/class
Exception Location: /usr/local/lib/python3.6/dist-packages/django/utils/module_loading.py in import_string, line 24
Python Executable: /usr/bin/python
Python Version: 3.6.3
Python Path:
['/home/sukumar/PycharmProjects/DjProject',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Sun, 4 Feb 2018 19:58:43 +0000