Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Add rows to MySQL database

So I already have a database setup with a few columns and a few rows already inserted in. I'm trying to create a view that you would just input information into a form and press Submit, then a row would be added to the MySQL database with the information you just typed in.

I believe you can do this with admin, but I would like to try without admin and I'm not sure if this is possible? I've been using the MySQL commandline to add rows as of now..

like image 506
yeenow123 Avatar asked Jul 08 '12 21:07

yeenow123


2 Answers

Of coures this is possible this is a building block for data driven websites. You can use a ModelForm as Daniel suggested (they offer built in validation and HTML markup for FREE) to easily map your model to a front end form. It would probably be beneficial to start with django tutorial or documentation first.

At the the very basic, all you have to do is instantiate your model

new_entry = YourModel(name='me', age='222', about='stackoverflow')

then save it

new_entry.save()

This adds it as a new row to your db.

https://docs.djangoproject.com/en/dev/topics/db/models/

like image 58
dm03514 Avatar answered Nov 03 '22 15:11

dm03514


Why would it not be possible?

You probably want a modelform (but see the general form introduction first).

like image 37
Daniel Roseman Avatar answered Nov 03 '22 15:11

Daniel Roseman