Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Saving Posted form data to database [RSS Feed]

#1 March 25, 2008 14:12:37

Mr T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Saving Posted form data to database


I am having a bit of trouble posting checked items from a form to my
view so I can save it to a database table.
The data in the form are information that has been stripped from a
file so there is no database link/referencing there.

What I have done half works as it does post my selected items and
saves it to the database but it only posts the last item in the list
rather than the other items I have selected.

It might just be me looking at this too long or I'm doing something
horribly wrong.

Any help would be great

Here is a snippet of my code that I am having problems with....

------Html form---------
{% for choice in currentproject %}
<form action="/lib/project/{{choice.id}}/" method="POST">
<table id="searchtable" name="searchtable">
<tr>
<th>Check Name:</th>
<th>Variable:</th>
<th>Error Message:</th>
<th>Range:</th>
<th>Comments:</th>
<th>Select:</th>
</tr>

{% if checkstore %}
{% for a,b,c in checkstore %}
<tr>
<td border=1 align="center"><input type="text"
readonly="True"
name="chname" value="{{a}}"/></td>
<td border=1 align="center"><input type="text"
readonly="True"
name="clflag" value="{{b}}"/></td>
<td border=1 align="left"><input type="text"
readonly="True"
name="ermsg" value="{{c}}"/></td>

<td border=1 align="center">
<input type="text" name="rangech" value="None"/>
</td>
<td border=1 align="center">
<input type="text" name="comment" value="None"/>
</td>
<td border=1 align="center">
<input type="checkbox" name="selectedcheck"/>
</td>

</tr>
{% endfor %}
{% endif %}
</table>
<div align="center">
<br/>
<input type="submit" value="Add to Checks to project"/>
</div>
</form>
{% endfor %}

-------view------------

if request.POST:
selected1 = Project.objects.get(id=project_id)

try:
selectedch = request.POST
except:
#stops any empty POST errors
qset = (Q(projectid = project_id))
results = StudyChecksFinal.objects.filter(qset)
return render_to_response('selected.html', {
'results': results,
'currentproject': currentproject,
'theuser' : theuser,
})

chname = request.POST
clflag = request.POST
ermsg = request.POST

try:
range2 = request.POST
except:
range2 = 'None'
try:
comments = request.POST
except:
comments = 'None'
if range2 == 'None':
range2 = ''
if comments == 'None':
comments = ''

new = StudyChecksFinal(projectid=selected1, checkname='%s' %chname,
clinflag='%s' %clflag, errmsg='%s' %ermsg, checkrange='%s' %range2,
comments='%s' %comments)
new.save()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 25, 2008 14:56:07

Evert R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Saving Posted form data to database


> I am having a bit of trouble posting checked items from a form to my
> view so I can save it to a database table.
> The data in the form are information that has been stripped from a
> file so there is no database link/referencing there.
>
> What I have done half works as it does post my selected items and
> saves it to the database but it only posts the last item in the list
> rather than the other items I have selected.
>
> It might just be me looking at this too long or I'm doing something
> horribly wrong.
>
> Any help would be great
>
> Here is a snippet of my code that I am having problems with....
>
> ------Html form---------
> {% for choice in currentproject %}
> <form action="/lib/project/{{choice.id}}/" method="POST">
> <table id="searchtable" name="searchtable">
> <tr>
> <th>Check Name:</th>
> <th>Variable:</th>
> <th>Error Message:</th>
> <th>Range:</th>
> <th>Comments:</th>
> <th>Select:</th>
> </tr>
>
> {% if checkstore %}
> {% for a,b,c in checkstore %}
> <tr>
> <td border=1 align="center"><input type="text"
> readonly="True"
> name="chname" value="{{a}}"/></td>
> <td border=1 align="center"><input type="text"
> readonly="True"
> name="clflag" value="{{b}}"/></td>
> <td border=1 align="left"><input type="text"
> readonly="True"
> name="ermsg" value="{{c}}"/></td>
>
> <td border=1 align="center">
> <input type="text" name="rangech" value="None"/>
> </td>
> <td border=1 align="center">
> <input type="text" name="comment" value="None"/>
> </td>
> <td border=1 align="center">
> <input type="checkbox" name="selectedcheck"/>
> </td>
>
> </tr>
> {% endfor %}


This will result in several checkboxes, all with the same value (well,
actually, no value at all); how are you going to distinguish between
the different checkboxes?
Did you check the actualy rendered HTML, and the POST variable you get
returned from this form?


>
> {% endif %}
> </table>
> <div align="center">
> <br/>
> <input type="submit" value="Add to Checks to project"/>
> </div>
> </form>
> {% endfor %}
>
> -------view------------
>
> if request.POST:
> selected1 = Project.objects.get(id=project_id)
>
> try:
> selectedch = request.POST

You'll only get the last value in this way. See the docs for
QueryDict:http://www.djangoproject.com/documentation/request_response/#querydict-objects>
> except:
> #stops any empty POST errors
> qset = (Q(projectid = project_id))
> results = StudyChecksFinal.objects.filter(qset)
> return render_to_response('selected.html', {
> 'results': results,
> 'currentproject': currentproject,
> 'theuser' : theuser,
> })
>
> chname = request.POST
> clflag = request.POST
> ermsg = request.POST
>
> try:
> range2 = request.POST
> except:
> range2 = 'None'
> try:
> comments = request.POST
> except:
> comments = 'None'
> if range2 == 'None':
> range2 = ''
> if comments == 'None':
> comments = ''
>
> new = StudyChecksFinal(projectid=selected1, checkname='%s' %chname,
> clinflag='%s' %clflag, errmsg='%s' %ermsg, checkrange='%s' %range2,
> comments='%s' %comments)
> new.save()


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support