Django how to modify database records by template
I want to delete the records which i select, and run.html will refresh, how can i do that? Since i use function run in views.py to send records in database, and run need one parameter build which can be got by using run.name, so i think i need to pass "run.name" and "run.id" when i click the submit button.
urls.py
urlpatterns = patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
(r'^home/$', 'views.home'),
(r'^home/(?P<build>[^/]+)/$', 'views.run'),
(r'^run/delete/$', 'views.runDelete')
)
run.html
<form name="form" method="post" action="/run/delete">
<input type="submit" value="Delete" style="margin-left:149px; width:80px; height:30px">
<table border="1"; borderColor=black>
<td></td>
<td><b>Run</b></td>
{% for run in run_list %}
<tr>
<td><input type="checkbox" name="var_delete" value="{{run.id}}"></td>
<td>{{run.name}}</td>
</tr>
{% endfor %}
</table>
</form>
views.py
def run(request, build):
run_list = TestRun.objects.all().order_by('id')
return render_to_response('run.html',开发者_运维百科 {'run_list': run_list})
def runDelete(request, id, build):
TestRun.objects.get(id=id).delete()
run()
i also want to ask if i select multiple record in run.html, whether should i write a forloop in runDelete to delete all of them?
thanks :D
You should definitely take a look at Django Form
精彩评论