DIRS無法導入site.views。錯誤是:無模塊命名視圖
./ksp
__init__.py
jquery.pngFix.js
manage.py
passenger_wsgi.py
settings.py
site/
site_media/
templates/
tmp/
urls.py
./ksp/site
__init__.py
models.py
models_.py
myforms.py
urls.py
views.py
views_back.py
FILES
./ksp/ passenger_wsgi.py:
import os, sys
sys.path.insert(0,'/usr/lib/python2.7/site-packages/django-1.3')
sys.path.append('/home/p/pravicom/ksp/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
./ksp/urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
('', include('site.urls')),
)
./ksp/site/urls.py:
import views
from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib.auth.views import login, logout
urlpatterns = patterns('',
(r'^$', 'views.ShowMain'),
#....
)
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
./ksp/site/ views.py:
from django.shortcuts import render_to_response
from models import *
from myforms import *
from django.http import HttpResponseRedirect, HttpRequest
from django import forms
from django.template import RequestContext
from django.contrib.auth.models import User
from django.contrib import auth
from django.contrib.auth.decorators import login_required
import os, urlparse, Image, sys
from django.core.paginator import Paginator, InvalidPage, EmptyPage
def custom_proc(request):
return{
'user': request.user
}
def ShowMain(request):
all_posts = Posts.objects.all().order_by('-id')
return render_to_response("index.html", {'all_posts':all_posts}, context_instance=RequestContext(request, processors=[custom_proc]))
./ksp/settings.py:
...
ROOT_URLCONF = 'urls'
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
# 'django.contrib.sites',
# 'django.contrib.flatpages',
'django.contrib.comments',
'site',
)
...
結果:
異常類型:ViewDoesNotExist 異常值:
無法導入site.views。錯誤是:沒有名爲視圖模塊
你能告訴我們ksp/site/views.py的內容嗎? – BobS
是的。查看更新。 – user2009490
我用登錄模塊做了這個。下面解決了這個問題。 – Ben