【问题标题】:Django 1.6: UnboundLocalError in formsDjango 1.6:表单中的 UnboundLocalError
【发布时间】:2014-08-11 15:13:26
【问题描述】:

我收到这个 unboundLocal 错误,上面写着 赋值前引用的局部变量“genderselect”

在 view.py 的第 131 行

gend = Doctor.objects.get(gender = genderselect)

我在两个不同的模板中有相同的表单

索引.html

<div class="signup">
          <div class="form-group">
            <form action="" method="post" >
            <select class="form-control" id="selection" name="selection">
              <option><b>Find a Doctor...</b></option>
              {% for value, text in form.selection.field.choices %}
                <option value="{{ value }}">{{ text }}</option>
              {% endfor %}
              {% csrf_token %}
            </select>

            <span class="input-group-btn">
              <button class="btn btn-primary" type="submit"  name="submit" id="ss-submit">Find Doctors</button>
            </span>
          </form>
          </div>
        </div>

doclisting.html

<select class="form-control" id="selection" name="selection">
              <option><b>Find a Doctor...</b></option>
              {% for value, text in form.selection.field.choices %}
                <option value="{{ value }}">{{ text }}</option>
              {% endfor %}
            </select>

              <select class="form-control" id="genderdropdown" name="genderdropdown">
              <option><b>Select a Gender</b></option>
              {% for value, text in form.genderselect.field.choices %}
                <option value="{{ value }}">{{ text }}</option>
              {% endfor %}
            </select>

            <span class="input-group-btn">
              <button class="btn btn-primary" type="submit"  name="submit" id="ss-submit">Search</button>
            </span>
          </div>
          {% csrf_token %}

        </form>

这是我收到错误的视图

def doclistings(request):
    d = getVariables(request)
    if request.method == "POST":
        form = DropdownSelectionForm(request.POST)
        print form.errors

        if form.is_valid():
            selection = form.cleaned_data['selection']
            genderselect = form.cleaned_data['genderselect']
            d['usergendselect'] = genderselect
            request.session["selection"] = request.POST['selection']

            return HttpResponseRedirect('/doclistings')
    else:
        form = DropdownSelectionForm()


    s_name = request.session.get('selection')  # Change variable name
    d['userselection'] = s_name  # Update this for new variable name
    gend = Doctor.objects.get(gender = genderselect)
    spec = Specialization.objects.get(name=s_name)  # Get spec object
    doctors = Doctor.objects.filter(specialization = spec, gender = gend).order_by('-likes')  
    d['doctors'] = doctors

    d.update({'form': form})
    return render_to_response('meddy1/doclistings.html',d)

我猜我的表单没有通过验证。我不知道为什么会这样。

这是我正在使用的表格

class DropdownSelectionForm(forms.Form):
    selection = forms.ChoiceField(choices=MY_CHOICES, widget = forms.Select, required = False)
    genderselect = forms.ChoiceField(choices=GENDER_CHOICES, widget= forms.Select, required = False)

【问题讨论】:

    标签: python django forms views


    【解决方案1】:

    request.method == 'GET' 未设置变量genderselect 时。仅在提交表单时设置,当时request.method == 'POST'

    您可能希望重新构建代码,以便为GET 请求适当地设置该变量。

    【讨论】:

      【解决方案2】:

      线

      gend = Doctor.objects.get(gender = genderselect)
      

      使用变量genderselect,该变量仅在请求方法为 POST 时存在。意思是,正常加载页面时,您会得到 local variable 'genderselect' referenced before assignment - 因为您使用的是未声明的变量(python 识别代码的其他部分定义了 genderselect 并假设您在分配之前使用它,因此出现错误) .

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-12
        • 1970-01-01
        • 2014-08-17
        • 2021-06-15
        • 1970-01-01
        相关资源
        最近更新 更多