Django功能视图跳过循环?

时间:2015-02-09 15:10:09

标签: python django apache django-views django-class-based-views

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

thunderdome /浏览次数

return HttpResponse

^^^跳过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_form.html,当真正发生的事情是它跳过循环并直接转到'Paste'

网址

return HttpResponse(statusly)

thunderdome / test_forms.html

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'),

错误日志:

                <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

为什么会这样?我遗漏了基于函数的视图与基于类的视图的内容吗?

1 个答案:

答案 0 :(得分:1)

您实际上并未在if块内返回HttpResponse语句生成的render。尝试更改:

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

为:

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

正如您在the documentation中看到的那样,render方法返回HttpResponse个对象。您正在放弃该响应并进入return HttpResponse(statusly)声明。