所以我有一个名为 index 的视图,它可能会收到也可能不会收到 POST 表单。我在这里看到过这样的问题,并注意到大多数时候它似乎是视图中的一个简单的逻辑错误,但是我写视图的方式,渲染调用在所有条件之外,被提取以便它应该总是被调用。观点是这样的:
from django.shortcuts import render, render_to_response, redirect
from django.template import RequestContext
from django.http import HttpResponse, Http404
import datetime
myUsers = ['A', 'B', 'C', 'D', 'E', 'Guest']
def index(request):
# grab user if we've submitted login form from this page
if request.method == 'POST':
if 'myUser' in request.POST:
request.session['myUser'] = request.POST['myUser']
# make login default guest if not logged in
elif not 'myUser' in request.session:
request.session['myUser'] = 'Guest'
return render(request, 'home/index.html', {'myUsers': myUsers})
我对解决方案相当坚持,我们将不胜感激!
编辑:这也是追溯。
Environment:
Request Method: GET
Request URL: http://transcode50.com/home/
Django Version: 1.5.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',
'django.contrib.admin',
'bootstrap_toolkit',
'transcode',
'transcode50',
'home',
'books')
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
133. raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name))
Exception Type: ValueError at /home/
Exception Value: The view home.views.index didn't return an HttpResponse object.