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()
- [Answered ]-How can I provide an API to get the History of changes made in any model in django-rest?
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.
Source:stackexchange.com