我正在尝试将我的文本值从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.py
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.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)
]
我正在发布我的HTML和views.py供参考。你们中的任何人都可以指出我的错误来帮助我。
答案 0 :(得分:0)
将此属性添加到您的表单中:
enctype="multipart/form-data"
像这样:
<form action="" method="post" enctype="multipart/form-data">
注意在视图端文件字段中使用request.FILES