Skip to main content
 首页 » 编程设计

python之在 Django 中找不到 View

2025年05月04日22lexus

我遇到了错误

ViewDoesNotExist 在/ 无法导入 blog.views。模块博客中不存在 View 。

使用 Django

我的 URLS 文件是这样的

from django.conf.urls import patterns, include, url 
 
# Uncomment the next two lines to enable the admin: 
# from django.contrib import admin 
# admin.autodiscover() 
 
urlpatterns = patterns('', 
    # Examples: 
    url(r'^$', 'blog.views.home', name='home'), 
    # url(r'^Blog/', include('Blog.foo.urls')), 
 
    # Uncomment the admin/doc line below to enable admin documentation: 
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 
 
    # Uncomment the next line to enable the admin: 
    # url(r'^admin/', include(admin.site.urls)), 
) 

我的目录树是这样的

Blog 
_manage.py 
_Blog 
__wsgi.py 
__urls.py 
__settings.py 
__ __init__.py 
_blog 
__views.py 
__tests.py 
__Templates 
____index.html 
__models.py 
__ __init__.py 

在 views.py 中

from django.shortcuts import render_to_response 
 
from blog.models import posts 
 
def home(request): 
    return render_to_response('index.html') 

进一步调试:

Environment: 
 
 
Request Method: GET 
Request URL: http://192.168.2.3:1337/ 
 
Django Version: 1.4.1 
Python Version: 2.7.3 
Installed Applications: 
('django.contrib.auth', 
 'django.contrib.contenttypes', 
 'django.contrib.sessions', 
 'django.contrib.sites', 
 'django.contrib.messages', 
 'django.contrib.staticfiles') 
Installed Middleware: 
('django.middleware.common.CommonMiddleware', 
 'django.contrib.sessions.middleware.SessionMiddleware', 
 'django.middleware.csrf.CsrfViewMiddleware', 
 'django.contrib.auth.middleware.AuthenticationMiddleware', 
 'django.contrib.messages.middleware.MessageMiddleware') 
 
 
Traceback: 
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 
  101.                             request.path_info) 
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve 
  300.                     sub_match = pattern.resolve(new_path) 
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve 
  209.             return ResolverMatch(self.callback, args, kwargs, self.name) 
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in callback 
  216.         self._callback = get_callable(self._callback_str) 
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in wrapper 
  27.         result = func(*args) 
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in get_callable 
  101.                     (lookup_view, mod_name)) 
 
Exception Type: ViewDoesNotExist at / 
Exception Value: Could not import blog.views. View does not exist in module blog. 

请您参考如下方法:

根据您的调试输出,blog 应用程序似乎不存在于 settings.py 中的 INSTALLED_APPS 设置中