Paulo
Paulo

Reputation: 7362

django forms into database

hey guys, im trying to make a volunteer form, that takes info such as name, last name, etc. and i want to save that info into my database (MySQL), so that it can be retrieved later on .

Upvotes: 0

Views: 117

Answers (1)

Josiah
Josiah

Reputation: 4854

So first you'll need to define a model that will hold this information, in the models.py file something like:

class Volunteer(models.Model):
    def __unicode__(self):
        return self.fname + self.lname
    fname = models.CharField(max_length=200)
    lname = models.CharField(max_length=200)
    bio = models.TextField(max_length=400)
    number = models.CharField(max_length=15)
    email = modesl.CharField(max_length=255)

And then a view to receive the POST data from the form, in views.py:

def volunteer_create(request):
    if request.method == 'POST'and request.POST['fname'] and request.POST['lname'] and request.POST['email'] (ETC...):
        v = Volunteer()
        v.fname = request.POST['fname']
        v.fname = request.POST['lname']
        v.fname = request.POST['email']
        v.fname = request.POST['number']
        ...
        v.save()
        return HttpResponse("Thank you!")    #success!
    else
        return HttpResponseRedirect("/volunteer_form/") #take them back to the form to fill out missed info

Then you'll need to set up your urls.py to point the form target to this view.

Upvotes: 2

Related Questions