0

I have a form in which I can input text through text boxes. How do I make these data go into the db on clicking submit.

this is the code of the form in the template.

<form method="post" action="app/save_page">
<p>
Title:<input type="text" name="title"/>
</p>
<p>
Name:<input type="text" name="name"/>
</p>
<p>
Phone:<input type="text" name="phone"/>
</p>
<p>
Email:<input type="text" name="email"/>
</p>
<p>
<textarea name="description" rows=20 cols=60>
</textarea><br>
</p>
<input type="submit" value="Submit"/>
</form>

I have a function in the views.py for saving the data in the page. But I dont know how to impliment it properly:

def save_page(request):
   title = request.POST["title"]
   name = request.POST["name"]
   phone = request.POST["phone"]
   email = request.POST["email"]
   description = request.POST["description"]

Now how do I send these into the db?

And what do I put in views.py so that those data goes into the db? so how do I open a database connection and put those into the db and save it?

should I do something like :

connection=sqlite3.connect('app.db')
cursor= connection.cursor()
.....
.....
connection.commit()
connection.close()

Thank you.

3 Answers 3

6

Django will do this for you, but you need to create a form. You should read about forms in Django and creating forms from models.

It's not necessary to accomplish this particular task, but you should also read about Django ORM.

Try reading the official documentation or one of the Django books (I learned Django from "Practical Django projects" and "The Django Book").

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

Comments

2

This question is asking for a brief description of the whole Djando DB model.

The short answer is you don't connect to the DB from the view, you have to use Django DB API. You have to define the model class in your models.py and then use class methods to update the data. Django will take care of passing/storing that in the DB.

Anticipating your question 'how?' I unfortunately will have to point you to the official DJango documentation.

Update: Another good resource about Django with comments, notes and examples.

3 Comments

thanks for the reply. Exactly.."how" is my question. The django does seems to be so un friendly for a beginner. Looks like its just a reference for the pros..They don't explain with one single example but keep jumping here & there. Anyways I'll have to manage with that.
I see what you mean. It does require some level of digging around and trying out things before it all starts to make sense. I have update my answer with another link, which may be useful when starting with Django. Once you get used to it, it's not so unfriendly! :)
ok. I will read through The django book ... would have been really helpful if they had something for django like railscast for rails :)
0

You can save send data into the django databse easily by working with django forms. Django forms make saving db data easily by linking your forms to your models through the ModelForm. Read the documention on django forms, modelforms, and the ORM. The following links may help you. I use django 1.9 at the moment

  1. https://docs.djangoproject.com/en/1.9/topics/forms/
  2. https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/#modelform

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.