尝试将数据从下拉列表获取到Django

时间:2019-12-04 11:16:23

标签: django django-forms django-views

我想从HTML下拉列表中将数据获取到views.py文件中,以便可以从数据库中获取从下拉列表中获取的数据。

第一次尝试

错误:

Request Method:     POST<br/>

Request URL:    http://127.0.0.1:8000/admissions/<br/>

Django Version:     2.2.6<br/>
Exception Type:     MultiValueDictKeyError<br/>

Exception Value:    

'state_dropdown'

Exception Location:     C:\django\lib\site-packages\django\utils\datastructures.py in __getitem__, line 80<br/>
Python Executable:  C:\django\Scripts\python.exe<br/>
<br/>

先谢谢了。

forms.py

class state_form(Form):
    state_choices = (
        ("Delhi", "Delhi"),
        ('west bengal', 'West Bengal'),
        ("Andhra Pradesh", "Andhra Pradesh"),
        ("Arunachal Pradesh", "Arunachal Pradesh"),
        ("Assam", "Assam"), ("Bihar", "Bihar"),
        ("Chhattisgarh", "Chhattisgarh"),
        ("Goa", "Goa"),
        ("Gujarat", "Gujarat"),
        ("Haryana", "Haryana"),
        ("Himachal Pradesh", "Himachal Pradesh"),
        ("Jharkhand", "Jharkhand"),
        ("Karnataka", "Karnataka"),
        ("Kerala", "Kerala"),
        ("Madhya Pradesh", "Madhya Pradesh"),
        ("Maharashtra", "Maharashtra"),
        ("Manipur", "Manipur"),
        ("Meghalaya", "Meghalaya"),
        ("Mizoram", "Mizoram"),
        ("Nagaland", "Nagaland"),
        ("Odisha", "Odisha"),
        ("Punjab", "Punjab"),
        ("Rajasthan", "Rajasthan"),
        ("Sikkim", "Sikkim"),
        ("Tamil Nadu", "Tamil Nadu"),
        ("Telangana", "Telangana"),
        ("Tripura", "Tripura"),
        ("Uttar Pradesh", "Uttar Pradesh"),
        ('Uttarakhand', 'Uttarakhand'),
)
state_name=forms.ChoiceField(choices=state_choices)

views.py

def admissions(request):
if request.method == "POST":

    a = request.GET['state_dropdown']
    print(str(a))
else:
    context_dict = {}
    return render( request, 'buddyscholarship_html/admissions.html',{})

HTML代码          

    <form class="form-control mb-3" style="margin-top:500px;" method="post">
        {% csrf_token %}
        <select class="col-lg-7 mb-4 mb-lg-0" name="state_dropdown">
            <option value="select your state">select your state</option>
            <option value="Uttarakhand">Uttarakhand</option>
            <option value="West Bengal">West Bengal</option>
            <option value="Andhra Pradesh">Andhra Pradesh</option>
            <option value="Arunachal Pradesh">Arunachal Pradesh</option>
            <option value="Assam">Assam</option>
            <option value="Bihar">Bihar</option>
            <option value="Goa">Goa</option>
            <option value="Gujarat">Gujarat</option>
            <option value="Haryana">Haryana</option>
            <option value="Himachal Pradesh">Himachal Pradesh</option>
            <option value="Jharkhand">Jharkhand</option>
            <option value="Karnataka">Karnataka</option>
            <option value="Kerala">Kerala</option>
            <option value="Madhya Pradesh">Madhya Pradesh</option>
            <option value="Maharashtra">Maharashtra</option>
            <option value="Manipur">Manipur</option>
            <option value="Meghalaya">Meghalaya</option>
            <option value="Mizoram">Mizoram</option>
            <option value="Nagaland">Nagaland</option>
            <option value="Odisha">Odisha</option>
            <option value="Punjab">Punjab</option>
            <option value="Rajasthan">Rajasthan</option>
            <option value="Sikkim">Sikkim</option>
            <option value="Tamil Nadu">Tamil Nadu</option>
            <option value="Telangana">Telangana</option>
            <option value="Tripura">Tripura</option>
            <option value="Delhi">Delhi</option>
            <option value="Uttar Pradesh">Uttar Pradesh</option>
        </select>
        <input type="submit" value="submit" class="btn btn-primary">Search college</input>
    </form>
    </section>

</div>

然后我已经尝试过:

views.py

def admissions(request):
if request.method == "POST":

    a = request.GET.get('state_dropdown')
    print(str(a))
else:
    context_dict = {}
    return render( request, 'buddyscholarship_html/admissions.html',{})

我遇到的错误

ValueError at /admissions/<br/>

The view buddyscholarship.views.admissions didn't return an HttpResponse object. It returned None instead.<br/>

Request Method:     POST<br/>
Request URL:    http://127.0.0.1:8000/admissions/<br/>
Django Version:     2.2.6<br/>
Exception Type:     ValueError<br/>
Exception Value: <br/>  

视图buddyscholarship.views.admissions没有返回HttpResponse对象。它返回None。

2 个答案:

答案 0 :(得分:0)

寻找request.POST请求时,应使用POST字典:

def admissions(request):
    if request.method == "POST":
        a = request.POST.get('state_dropdown')
        print(str(a))
        return HttpResponse('POST request')
    else:
        context_dict = {}
        return render( request, 'buddyscholarship_html/admissions.html',{})

还要记住,您应该始终返回HttpReponse(或子类)而不是无(第二个错误是在抱怨这一点)。

答案 1 :(得分:0)

您需要使用request.POST而不是request.GET,因为它是您要发送的POST请求。像这样。

print (df)
   Negative  Positive  Neutral
0      True     False     True
1      True     False    False
2     False     False     True
3     False      True    False
4      True     False    False
5     False      True    False
6      True     False    False
7      True     False    False


df['Overall'] = df.dot(df.columns + ',').str.rstrip(',')
print (df)
   Negative  Positive  Neutral           Overall
0      True     False     True  Negative,Neutral
1      True     False    False          Negative
2     False     False     True           Neutral
3     False      True    False          Positive
4      True     False    False          Negative
5     False      True    False          Positive
6      True     False    False          Negative
7      True     False    False          Negative