Home > Software engineering >  How to solve the error when dummy data gets stored in Django database with its id as none and all va
How to solve the error when dummy data gets stored in Django database with its id as none and all va

Time:11-21

I had made this form in Django which takes some data and when a save button is clicked it saves it to the database. It stores the data that I give, but I noticed that every time I refreshed the page that contained the form it would automatically save an object with the Id as none and all values set to null. I wanted to know how to write a function that says the form only submits to the database when the save submit button is clicked and not when reloading the page and making a dummy object in the database.

CodePudding user response:

I think the problem here is in the view, more specifically, how you're handling the request inside the view. it seems that you're saving the object no matter what's the request method, that's why even refreshing the page will save a new object.

CodePudding user response:

You must add csrf_token inside the form!

As in the example below ...

<form method="post">
{% csrf_token %}
<input name="plan" id="id_plan" value="1">
<button>buy</button>
</form>
  • Related