0

I'm having some trouble sending data from a form to my database.

The form:

<form method="POST">
    {% csrf_token %}
    <div class="banner" style="border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px;">
      <h1 style="font-family: 'Baloo Chettan 2', cursive; font-size: 50px;">Title</h1>
    </div>
    <div class="colums" style="padding-left: 8px; padding-top: 20px;">
      <div class="item">
        <label for="i1"> Input One<span>*</span></label>
        <input id="i1" type="text" name="i1" placeholder="Data point 1" required/>
      </div>
      <div class="item">
        <label for="i2"> Input Two<span>*</span></label>
        <input id="i2" type="text" name="i2" placeholder="Data point 2" required/>
      </div>
      <div class="item">
        <label for="i3">Input Three<span>*</span></label>
        <input id="i3" type="text" name="i3" placeholder="Data point 3" required/>
      </div>
      <div class="item">
        <label for="i4">Input Four<span>*</span></label>
        <input id="i4" type="text" name="i4" placeholder="Data point 4" required/>
      </div>
    <div class="btn-block">
      <button type="submit" value="CalcData" style="width: 1052px; align-content: center; background-color: #3E828F;">Calculate</button>
    </div>
</form>

views.py:

def save_data(request):
    if request.method == 'POST':
        if request.POST.get('i1') and request.POST.get('i2') and request.POST.get('i3') and request.POST.get('i4'):
            data=CalcData()
            data.i1= request.POST.get('i1')
            data.i2= request.POST.get('i2')
            data.i3= request.POST.get('i3')
            data.i4= request.POST.get('i4')
            data.save()

            return render(request, 'home.html')  

    else:
        return render(request,'home.html')

models.py:

class CalcData(models.Model):
    i1 = models.DecimalField(max_digits = 100, decimal_places = 5)
    i2 = models.DecimalField(max_digits = 100, decimal_places = 5)
    i3 = models.DecimalField(max_digits = 100, decimal_places = 5)
    i4 = models.DecimalField(max_digits = 100, decimal_places = 5)
    datetime = models.DateTimeField(default=timezone.now)

This POST doesn't end up in my database, so I'm not sure where it's going. I would assume the problem is in my views code somewhere but I'm not sure where.

2 Answers 2

1

add the action in your form and add the url. like this:

<form method="post" action="{% url 'YOUR_APP_NAME: save_data' %}">
Sign up to request clarification or add additional context in comments.

Comments

1

The POST data is posted to the view handling the form page. If its not the view handling the form processing (which it seems), then you should make some changes to your form action attribute.

point it to the url that points to the save_data view function.

Comments

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.