request.POST.get返回NULL

问题描述 投票:0回答:1

我正在尝试将我的文本值从我的HTML表单发布到Django视图,但结果总是得到NONE。

最初,我试图通过request.POST获取值,它抛出“MultiValueDictKeyError”,然后我将我的代码更改为request.POST.get,它返回None

HTML文件

{%csrf_token%}

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1" style="margin-top: 5px">

            <ul class="nav navbar-nav">

                <li class="dropdown" style="padding-right: 10px">
                    <select class="form-control" id="sel1" style="font-family: Courier">
                        {% for environment in env_list %}
                            <option name="NewEnvName"
                                    value="{{ environment.Env_name }}">{{ environment.Env_name }}</option>
                        {% endfor %}
                    </select>
                </li>
            </ul>
            <ul class="nav navbar-nav">
                <li class="dropdown" style="font-family: Courier;font-size: 20px;color: ivory">

                    <input type='radio' name='CobranSelection' value="CobrandName" checked/>
                    <label for="edit-radios-lorem-tortor-erat" class="option"
                    >Cobrand
                        Name | </label>
                    <input type='radio' name='CobranSelection' value="CobrandId"/>
                    <label for="edit-radios-lorem-tortor-erat" class="option"
                    >Cobrand
                        Id </label>

                </li>
            </ul>


            <div class="form-group">
                <input type="text"
                       style="margin-top: -1px;font-family: Courier;width: 210px;margin-right: -10px"
                       class="form-control"
                       name="cobrand" placeholder="Eneter Cobrand detail here">
                <button type="submit" class="btn btn-default"
                        style="margin-top: -31px;font-family: Courier;margin-left: 230px">
                    Submit
                </button>
            </div>


        </div><!-- /.navbar-collapse -->
    </form>

view.朋友

    from django.shortcuts import render, HttpResponseRedirect
    from .models import Environments
    from django.db.models import Q
    import cx_Oracle



def knowcobrand(request):
    value_type = request.POST.get('CobranSelection')
    cobrand_value = request.POST.get('cobrand')
    env = request.POST.get('NewEnvName')
    print(value_type)
    print(cobrand_value)
    print(env)
    return render(request, 'CobrandView/CobrandInDepth.html')

URLs.朋友

from django.conf.urls import url
from . import views

urlpatterns = [
    url('login', views.login),
    url('Home', views.fetch_all_env),
    url('AddEnvironment', views.add_environment),
    url('GetEnvironment', views.GetEnvironment),
    url('ReadDb', views.readdb),
    url('LearnCobrand', views.learncobrand),
    url('ReadCobrand',views.knowcobrand)
]

我发布了我的HTML和views.py以供参考。你们中的任何人都可以指出我的错误来帮助我。

python html django django-forms html-form-post
1个回答
0
投票

将此属性添加到您的表单:

enctype="multipart/form-data"

像这样 :

<form action="" method="post" enctype="multipart/form-data">

并注意在视图侧文件字段中使用request.FILES

© www.soinside.com 2019 - 2024. All rights reserved.