我没有定义myproject“myapp”中的Django Basics myapp映射

时间:2016-04-24 13:12:50

标签: django

我正在学习django,并运行python manage.py startapp myapp,创建了以下文件夹结构:

myapp/
   __init__.py
   admin.py
   models.py
   tests.py
   views.py

我还在设置中将myapp添加到了INSTALLED_APPS

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

然后我运行了python manage.py migratepython manage.py createsuperuser来创建超级用户

我还创建了myapp的视图:

"from django.http import HttpResponse

def hello(request):
   text = """<h1>welcome to my app !</h1>"""
   return HttpResponse(text)
"

最后,这是URL映射:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
   url(r'^admin', include(admin.site.urls)),
   url(r'^hello/', 'myapp.views.hello', name = 'hello'),
)

运行时,会抛出错误,指出未定义“myapp”。我无法访问http://127.0.0.1:8000/admin

的管理页面

如何解决此错误并使我的应用程序正常运行?

2 个答案:

答案 0 :(得分:0)

可能myapp未包含在INSTALLED_APPS的{​​{1}}中。我想你正在关注this教程。

答案 1 :(得分:0)

将此添加到您的网址映射中:

from myapp import views