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 .
+1
A:
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.
Josiah
2010-07-20 19:12:24
thank you so much for your answer, if i make the model , do i still need to make a subclass of forms.Form ?
paulo
2010-07-20 19:18:31
I would recommend you use a `ModelForm` instead. http://docs.djangoproject.com/en/dev/topics/forms/modelforms/
Alasdair
2010-07-20 19:29:59
Thanks Alasdair, I'm still getting used to how much Django does "Automagically"
Josiah
2010-07-20 20:04:28