[Answered ]-How to store data from html form to postgres database using Django 1.10?

0👍

Since you made mysite/jobs/forms.py, you can use that in your HTML template:

<form action="{% url 'jobs:cost' %}" method="post">    {% csrf_token %}

    {{ form.as_p }}
    <input type="submit" value="Submit"/>

</form>

The reason why your form wasn’t saving is probably because of your mysite/jobs/views.py. Since you don’t need to add additional data besides the date and cost in the form, you can go ahead and save it instead of creating cost_obj:

if request.method == 'POST':

    form = CostForm(request.POST)

    if form.is_valid():

        form.save()

If you do want to create cost_obj, do it this way:

cost_obj = form.save(commit=False)

You can then add additional information into the object before saving it. e.g.:

cost_obj.user = request.user
cost_obj.save()

2👍

Your form is presumably not valid.

You should use {{ form.errors }} in the template to display the validation errors. Also, you should use {{ form.date }} and {{ form.cost }} to display the fields, rather than creating input tags manually, so that the values are re-populated when validation fails.

Leave a comment