Reputation: 107
I am new to django,I want to store the data which I enter into my html text tags to be stored in my SQLite database,I know I'm missing out on some code so please suggest me.
Index.html
<html>
<head>
<header style="background-color:blue;">
<h1 >Test template</h1></header>
<script>
function myfunc()
alert("data saved")
</script>
</head>
<body style="background-color:#FFD700">
<p>
<form>
question: <input type="text" name="question"><br>
answer: <input type="text" name="answer"><br>
<button type="button" onclick="myfunc()">save</button>
</form>
</p>
</center>
</body>
</html>
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render_to_response
def Index(request):
return render_to_response('Index.html')
models.py
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
answer = models.CharField('max_length=200')
def __unicode__(self):
return self.question
Upvotes: 0
Views: 2537
Reputation: 51988
You can try like this. In template:
<form action="/url" method="post">
{% csrf_token %}
question: <input type="text" name="question"><br>
answer: <input type="text" name="answer"><br>
<button type="button" onclick="myfunc()">save</button>
</form>
In views.py:
def Index(request):
new_poll= Poll() #call model object
d= request.POST
new_poll.question= d['question']
new_poll.question= d['answer']
new_poll.save()
return render_to_response('Index.html')
Upvotes: 1