Django 函数视图跳过循环?

Django Function View Skips Loop?

出于某种原因,在我的 Bartertown 函数中它完全跳过了 if 循环。我打印出每个变量以在日志中显示第一个变量 "Paste" 应该使用 thunderdome/paste_formdomain/Thunderdome/Bartertown 呈现一个新页面,当它实际上忽略循环并直接进入 return HttpResponse

thunderdome/Views

class SquirrelView(View):
    def post (self, request):
        form = request.POST.get("thundersubmit", "")
        if form  == '1':
            request.session["_thundersubmit"] = 'Pool'
            return HttpResponseRedirect('Bartertown')
        if form  == '2':
            request.session["_thundersubmit"] = 'Paste'
            return HttpResponseRedirect('Bartertown')
        if form  == '3':
            request.session["_thundersubmit"] = 'Upload'
            return HttpResponseRedirect('Bartertown')


        return HttpResponseRedirect('home')

def Bartertown(request):
    statusly = request.session.get('_thundersubmit')
    print statusly
    if statusly == 'Paste':
        render(request, 'thunderdome/paste_form.html')
    return HttpResponse(statusly)

^^^ 跳过 If 语句,如在错误日志中所见,'Paste' 应该有 "hit" 该循环并呈现 paste_form.html,当真正发生时是不是跳过了循环直接进入了return HttpResponse(statusly).

网址

from datetime import datetime
from django.conf.urls import patterns, url
from app.forms import BootstrapAuthenticationForm
from thunderdome.views import SquirrelView, Bartertown

# Uncomment the next lines to enable the admin:
from django.conf.urls import include
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'app.views.home', name='home'),
    url(r'^contact$', 'app.views.contact', name='contact'),
    url(r'^about', 'app.views.about', name='about'),
    url(r'^Thunderdome', include('thunderdome.urls')),
    url(r'^Thunderdome/squirrel', SquirrelView.as_view()),
    url(r'^Thunderdome/Bartertown', 'thunderdome.views.Bartertown'),

thunderdome/test_forms.html

                <form id="checkout-form" class="smart-form" novalidate="novalidate" action="/Thunderdome/squirrel" method="POST">{% csrf_token %}
                    <fieldset>
                        <section>
                            <div class="well">
                                <button type="submit" name="thundersubmit" class="btn btn-primary btn-lg btn-block"  value = "1">
                                    Choose Pool
                                </button>
                                <button type="submit" name="thundersubmit" class="btn btn-primary btn-lg btn-block" value = "2">
                                    Paste-A-Config
                                </button>
                                <button type="submit" name="thundersubmit" class="btn btn-primary btn-lg btn-block" value = "3">
                                    Upload-A-Config
                                </button>
                            </div>
                        </section>
                    </fieldset>
                </form>

错误日志:

[Mon Feb 09 08:48:50 2015] [notice] Apache/2.2.15 (Unix) DAV/2 PHP/5.3.3 mod_wsgi/3.5 Python/2.7.9 configured -- resuming normal operations
[Mon Feb 09 08:48:57 2015] [error] Paste
[Mon Feb 09 08:49:00 2015] [error] Pool
[Mon Feb 09 08:49:02 2015] [error] Paste

访问日志:

10.0.0.1 - - [09/Feb/2015:08:49:00 -0600] "GET /Thunderdome/Bartertown HTTP/1.1" 200 4 "http://10.0.0.2/Thunderdome" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
10.0.0.1 - - [09/Feb/2015:08:49:01 -0600] "GET /Thunderdome HTTP/1.1" 200 30140 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
10.0.0.1 - - [09/Feb/2015:08:49:02 -0600] "POST /Thunderdome/squirrel HTTP/1.1" 302 - "http://10.0.0.2/Thunderdome" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
10.0.0.1 - - [09/Feb/2015:08:49:02 -0600] "GET /Thunderdome/Bartertown HTTP/1.1" 200 5 "http://10.0.0.2/Thunderdome" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"

为什么会这样?我缺少有关基于函数的视图与基于 class 的视图的一些信息?

您实际上 return if 块内的 render 语句生成的 HttpResponse。尝试更改:

if statusly == 'Paste':
    render(request, 'thunderdome/paste_form.html')

至:

if statusly == 'Paste':
    return render(request, 'thunderdome/paste_form.html')

如您在 the documentation 中所见,render 方法 return 是一个 HttpResponse 对象。您正在放弃该响应并进入 return HttpResponse(statusly) 语句。