2

This is my view.So I want to keep 2 different HTML forms in same view ,But I am unable to do so beacuse whenever I add 1 form , I get the error of other on being none.

def home(request):
    name = None
    if request.method == 'POST':
        name = request.POST.get('name')
        choices = request.POST.get('choices')
        subtest = request.POST.get('subtest')
        reference = request.POST.get('reference')
        unit = request.POST.get('unit')
        test = Test()
        test.name = name
        test.save()
        subtest = Subtest()
        subtest.test = Test.objects.get(name=choices)
        subtest.name = subtest
        subtest.unit = unit
        subtest.reference_value = reference
        subtest.save()
        # print(name)
        return redirect('home')
    return render(request,'main.html',{}) 

I have got 2 different forms . I didn't use django forms because I wanted to try something new.

MY FIRST FORM

<form method="POST">
        {% csrf_token %}
        <div class="icon-holder">
          <i data-modal-target="test-popup" class="icon-cross"></i>
        </div>
        <div class="input-group">
          <input type="text" name="name" placeholder="Test name" />
        </div>
        <div class="button-group">
          <button type="submit">Submit</button>
        </div>
      </form>

MY SECOND FORM

<form method="POST">
        {% csrf_token %}
        <div class="icon-holder">
          <i data-modal-target="menu-test-popup" class="icon-cross"></i>
        </div>
        <div class="input-group">
          <label for="test-select">Test Name:</label>
          <select name="choices" id="test-select">
            {% for test in test %}
            <option value="{{test.name}}" name='choices'>{{test.name|title}}</option>
            {% endfor %}
          </select>
        </div>
        <div class="input-group">
          <input type="text" name="subtest" placeholder="SubTest name" />
        </div>
        <div class="input-group">
          <input type="text" name="reference" placeholder="Reference rate" />
        </div>
        <div class="input-group">
          <input type="text" name="unit" placeholder="Unit" />
        </div>
        <div class="button-group">
          <button type="submit">Submit</button>
        </div>
      </form>
2
  • Can you show me the error that you are facing Commented Feb 15, 2020 at 13:18
  • @Lag11 IntegrityError at / NOT NULL constraint failed: lab_test.name Commented Feb 15, 2020 at 13:21

2 Answers 2

2

first form

<form method="POST">
...
<input name="form_type" value="first-form" type="hidden">
</form>

second form

<form method="POST">
...
<input name="form_type" value="second-form" type="hidden">
</form>

view function

def view(request):
  if method == "POST":

    form_type = request.POST.get('form_type')

    if form_type == "first-form":
      # first form's process
    elif form_type == "second-form":  
      #second form's process
Sign up to request clarification or add additional context in comments.

3 Comments

I have already done this. I wanted to have something new like django forms where we can have 2 forms with 1 view and 1 url
then add extra hidden input named "form_type" to both forms one's value is "first-form" other's ''second-value". form_type = request.POST.get('form_type') if form_type == "first-form": #Do first form process else: # Do second form process
can you please explain and write some code for me . That would mean a lot
0

You have two forms here, when you submit the second form, only the fields from the second form gets submitted.

so from this line

name = request.POST.get('name')

name will become None. But I believe your Test model does not take any None value for name field( the reason for you " IntegrityError at / NOT NULL constraint failed: lab_test.name ").

To overcome this, first check if there is any value for 'name' then proceed with creating the test instance

if name:
    test = Test()
    test.name = name
    test.save()

Similarly check if the other fields are present for the second form before creating and saving the instance.

4 Comments

Let me break down the senario.. I got 2 different Html forms.I want both on one view and one urls. So if I submit 1 view ofcourse the other one is None.
yes so check the presence of the data before creating the instance since your Test model cannot save name as None
When I submit the second form I have none value for first form, Which raises error. Is there any way possible to ignore 1 form when we have other form filled
That is exactly what I have mentioned in the answer, "if name:" this first checks if the name has any value in it(it will be None if the second form is submitted). If and only if the name has some value(when the first form is submitted), test instance is created and saved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.