0

I'm trying POST my text value from my HTML form to Django view but always get NONE as a result.

Initially, I was trying to fetch value by request.POST which threw "MultiValueDictKeyError" then I changed my code to request.POST.get which is returning None

HTML file

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

I'm posting my HTML and views.py for reference. Could anyone of you please help me by pointing out my mistake.

Community
  • 1
  • 1
vivek.p.n manu
  • 286
  • 1
  • 4
  • 19
  • 2
    It would be great if you could let us know which specific part of your HTML file and view this is about. We can make assumptions based on your question text, but we should not *have to*. I still made an assumption, and it seems this question is a duplicate. You are trying to read from a field that is not submitted because it is disabled. – shmee Apr 05 '19 at 06:36
  • Possible duplicate of [values of disabled inputs will not be submitted?](https://stackoverflow.com/questions/1355728/values-of-disabled-inputs-will-not-be-submitted) – shmee Apr 05 '19 at 06:36
  • Thanks for your response. It worked fine after knowing that DOM fails to read disabled elements but I stuck passing value from dropdown selection to my view. Im getting None as a value even though I'm selecting a particular value. updated my HTML and view code. – vivek.p.n manu Apr 06 '19 at 15:42

1 Answers1

0

add this attribute to your form :

enctype="multipart/form-data"

like this :

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

and attention in view side file field use request.FILES

hassanzadeh.sd
  • 3,091
  • 1
  • 17
  • 26