首页 > 解决方案 > 当我在views.py中读取时,选定的下拉列表值为NULL

问题描述

我已经从用户那里获取了一些输入并将其存储在 Django SQLite 文件中,并且相同的数据正在获取它并显示在下拉列表中,但是当用户选择一个值时,所选值在 views.py 中显示为 null。

我也尝试过使用 POST.get 但仍然从表单中获取 NULL。你们中的任何人都可以帮我找出我的错误吗?

文件:

 <form method="post" action="/NewHandBook/ReadCobrand">{% 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">
                                    <option name="NewEnvName"
                                            value="xyz">xyz</option>
                            </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>

网址.py

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)
]

视图.py

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

标签: pythonhtmldjangodrop-down-menudjango-views

解决方案



推荐阅读