开发者

Django form pass parameter from template to view by submit button

开发者 https://www.devze.com 2023-01-12 03:50 出处:网络
I want to pass a parameter, when i click submit button. urls.py urlpatterns = patterns(\'\', (r\'^static/(?P<path>.*)$\', \'django.views.static.serve\', {\'document_root\': settings.MEDIA_ROOT

I want to pass a parameter, when i click 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>{{run.build}}</td>
        <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):
    run_list = reques开发者_运维问答t.POST.getlist('var_delete')
    for run in run_list:
        run = int(run)
        TestRun.objects.get(id=run).delete()
    return render_to_response('???')

i want to show new run.html, but how can i pass run.build as an parameter to runDelete? thanks:D


What is present in run.build?

For one, you can make it an input hidden field (other than displaying how you are displaying it already) and so, the value will be present in request.POST.

You can also make it the part of the url. But may not suit for your case.

0

精彩评论

暂无评论...
验证码 换一张
取 消