Django Python Dropdown提交失败

时间:2019-07-19 06:27:57

标签: python django dropdown

选择下拉列表并点击Go To时出错。看来它不是POST值回传错误。

在“视图”下,如果我添加以下内容,它将进入第二页,但未传递任何值:

class GatherDataView(FormView):
    context_object_name = 'location'
    template_name = 'gatherdata.html'
    form_class = SelectSITEForm

它不会传递值onclick提交按钮。

forms.py

class SelectSITEForm(forms.Form):

    LOCATIONS = (
        ('DC1', 'DC1'),
        ('DC2', 'DC2'),
    )
    location = forms.ChoiceField(choices=LOCATIONS, required=True)

    class Meta:
        model = SelectSITE

views.py

from .models import SelectSITE
from .forms import SelectSITEForm
class CheckView(FormView):

    template_name = "check.html"
    form_class = SelectSITEForm
class GatherDataView(FormView):

    def gatherdata(request):
        form = SelectSITEForm(request.POST or None)
        answer = ''
        if form.is_valid():
            location = form.cleaned_data.get('location')
        return render(request, 'gatherdata.html', {'form': form, 'location': location})

urls.py

urlpatterns = [
    url(r'^navbar/', views.NavPageView.as_view()), 
    url(r'^check/', views.checkView.as_view()),
    url(r'^gatherdata/', views.GatherDataView.as_view(), name='gatherdata'), 
]

html

<form method="post" action = "{% url 'gatherdata' %}"> {% csrf_token %}
  <div class="fieldWrapper" align="center" id="mainselection">
    <select name="location">
      <option selected>Select an the site</option>
      {% for val in form.location %}
      <option value="{{ val }}"></option>
      {% endfor %}
    </select>
  </div>
  <button align="center" class="button execute" name="submit" value="submit">GO TO</button></form>

onclick提交按钮HTML

{% extends "check.html" %}
{% block content %}
<br><br><br>
<h1>{{ location }}</h1>

跟踪:

URL: http://dpaste.com/3A0TFDS
Exception Type: TypeError at /gatherdata/
Exception Value: 'NoneType' object is not callable

0 个答案:

没有答案