views:

47

answers:

1

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

+1  A: 

You should definitely take a look at Django Form

Enrico Carlesso
thanks! But how can i get two parameters?By using post i can get the selected checkbox list, but i have no ideal about how to get build name...
Yuan
First, why you need build? I do not see it in your usage? And if it's a global parameter, add it to the url "/run/delete/<buildname>/" (take a look at {% url %} works.And what are the two params you need? run.id and run.name? Why do not you get it from r = TestRun.objects.get(id=id) and use r.name?
Enrico Carlesso
thanks everyone~
Yuan