1

I am new to Django , i just created a simple form to store the user input to database, but the data is not stored while clicking submit button, when i click the submit button the current page is refreshed, but data not stored in database. what is wrong in my code ?.I have created the following models, and my code is ,

mysite/jobs/models.py

from django.db import models

class Cost(models.Model):

    cost = models.FloatField()

    date = models.DateField()

mysite/jobs/views.py

from django.shortcuts import render

from jobs.forms import CostForm

from jobs.models import Cost

def costView(request):

    if request.method == 'POST':

        form = CostForm(request.POST)

        if form.is_valid():

            date = request.POST.get('date','')

            cost = request.POST.get('cost','')

            cost_obj = Cost(date=date, cost=cost)

            cost_obj.save()

            return HttpResponse("csdfsdf")

    else:

        form = CostForm()

    return render(request,'jobs/cost.html',{'form':form,})          

mysite/jobs/forms.py

from django import forms

class CostForm(forms.Form):

    date = forms.DateField()

    cost = forms.FloatField()

mysite/jobs/templates/jobs/cost.html

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

<p><label for="date">Date:</label><input type="text" name="date" value={% now "Y-m-d" %}/></p>

<p><label for="cost">Cost:</label><input type="text" name="cost" value="0" id="cost"/></p>

<input type="submit" value="Submit"/>

</form>

mysite/jobs/urls.py

from django.conf.urls import url


from . import views


urlpatterns = [
    url(r'^$', views.costView, name='cost'),
]

mysite/urls.py

from django.conf.urls import url, include

from django.contrib import admin


urlpatterns = [
    url(r'^', include('jobs.urls',namespace="jobs")),

    url(r'^polls/', include('polls.urls')),

    url(r'^admin/', admin.site.urls),

]
1
  • Hello dinesh. If either answer below is helpful to you, please select one as the correct answer. If they aren't, your comments or clarifications may assist people in helping you and be helpful to others with similar questions. :) Thanks! Commented Feb 5, 2017 at 14:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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()

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.