Ranjeet singh
Ranjeet singh

Reputation: 794

save values in database using django

i have an custom form , whenever i fetch the form values to save in the database than it display an error ( applicationform() got an unexpected keyword argument 'job_title' ) and the values are not save in the table.

views.py :-

def applicationvalue(request):
    if request.method == 'POST':

            getjobtitle = request.POST['jobtitle']


            getintable = applicationform(job_title=getjobtitle)
            getintable.save()

            print getjobtitle
            return HttpResponse(getintable)

    else:
        return render_to_response('registration/applicationform.html')

my form is :-

<form method="POST" action="#" class="form-horizontal" id="applicationform" name="appform">
<input type="text" id="u_jobtitle" class="input-xlarge" name="jobtitle" value=" " />
<button class="btn btn-gebo" type="submit" name="usubmit">Save changes</button>

whenever i fetch the values from form to save the values in table field " job_title " than it will display an error :-

applicationform() got an unexpected keyword argument 'job_title'

Upvotes: 2

Views: 5530

Answers (2)

Rohan
Rohan

Reputation: 53326

Change input field name to job_title in your html

<input name="job_title" type="text" id="u_jobtitle" class="input-xlarge" value=" " />
-------------^ changed 

and then in view do

def applicationvalue(request):
  if request.method == 'POST':
    #Dont need this
    #getjobtitle = request.POST['jobtitle']
    #---------------------------Use request.POST
    getintable = applicationform(request.POST)
    getintable.save()

    print getjobtitle
    return HttpResponse(getintable)
  else:
    return render_to_response('registration/applicationform.html')

It will be better if you use same form to render html instead of hand coding it.

Upvotes: 2

luc
luc

Reputation: 43096

The applicationform constructor should take the request.POST as argument. But it seems to me that you are not using django forms in the "right" way. I think that your view doesn't follow the django philosophy for using form.

In your case, you should have a model:

from django.db import models

class Application(models.Model):
    job_title = models.CharField(max_length=100)

Based on this model, you can declare a ModelForm:

from django import forms
from .models import ApplicationModel

class ApplicationForm(forms.ModelForm):

    class Meta:
        model = ApplicationModel
        fields = ('job_title',)

Then you can use this form in your view

def applicationvalue(request):
    if request.method == 'POST':

        form = ApplicationForm(request.POST)
        if form.is_valid():
            #This is called when the form fields are ok and we can create the object
            application_object = form.save()

            return HttpResponse("Some HTML code") # or HttResponseRedirect("/any_url")

    else:
        form = ApplicationForm() 

    #This called when we need to display the form: get or error in form fields
    return render_to_response('registration/applicationform.html', {'form': form})

finally you should have a registration/applicationform.html template with something like:

{% extends "base.html" %}

{% block content %}
<form action="" method="post">{% csrf_token %}
   <table>
      {{form.as_table}}
   </table>
   <input type="submit" value="Add">
</form>
{% endblock %}

I hope it helps

Upvotes: 2

Related Questions