Python Pass变量,该变量包含要调用的函数的名称,onclick=";location.href=';{%url';{{var}';%}'&引用;?
我正试图建立一个端口扫描django网站 问题陈述: 我需要的是,用户将选择单选按钮中的任何一个(完全扫描或快速扫描),然后当用户单击Python Pass变量,该变量包含要调用的函数的名称,onclick=";location.href=';{%url';{{var}';%}'&引用;?,python,django,django-forms,django-views,django-templates,Python,Django,Django Forms,Django Views,Django Templates,我正试图建立一个端口扫描django网站 问题陈述: 我需要的是,用户将选择单选按钮中的任何一个(完全扫描或快速扫描),然后当用户单击Start Now按钮时,应该从url.py of views.py调用python函数,单选按钮保存的值为 我在internet上看到,可以使用模板中的onclick=“location.href=”{%url”单选值变量“%”调用views.py的函数,但不能在url''处传递单选按钮值。 base.html <form id="suggest
Start Now
按钮时,应该从url.py of views.py调用python函数,单选按钮保存的值为
我在internet上看到,可以使用模板中的onclick=“location.href=”{%url”单选值变量“%”调用views.py的函数,但不能在url''处传递单选按钮值。
base.html
<form id="suggestion_form" name="gs" method="get" action="home">
<div class="searchText">
<input type="text" name="q" class="searchText" placeholder="{% block title %} {% endblock %}" readonly="readonly">
<ul>
<li><label><input type="radio" name="ext_com" value="Full Scan"><span>Full Scan</span></label></li>
<li><label><input type="radio" name="ext_com" value="Quick Scan"><span>Quick Scan</span></label></li>
</ul>
</div>
<input type="submit" name="results" class="main-button" value=" Start Now " onclick="location.href='{% url ' radio value variable ' %}'">
</form>
视图.py
from django.conf.urls import url
from . import views
urlpatterns = [
url('',views.home),
url('',views.fullscan,name="fullscan"),
url('',views.quickscan,name="quickscan"),
]
.....
def fullscan():
If fullscan radio button is selected then this should be called an output will be rendered
def quickscan():
If quickscan radio button is selected then this should be called an output will be rendered
我尝试过的解决方案-
What I did is
<script src="https://code.jquery.com/jquery-1.8.0.min.js"></script>
....
<div id="search-section">
<form id="suggestion_form" name="gs" >
<div class="searchText" >
<ul>
<li><label><input type="radio" name="ext_com" data-location="{% url 'fullscan' %}" autocomplete="off" checked>Full Scan</label></li>
<li><label><input type="radio" name="ext_com" data-location="{% url 'quickscan' %}" autocomplete="off">Quick Scan</label></li>
</ul>
</div>
<input type="button" id="startScanBtn" name="results" class="main-button" value=" Start Now ">
</form>
</div>
<script>
$("#startScanBtn").on('click', function(){
var location = $('.searchText:radio:checked').data('location');
window.location.href=location;
return false;
});
</script>
....
我所做的是
....
- 全扫描
- 快速扫描
$(“#开始扫描”)。在('单击',函数()上{
变量位置=$('.searchText:radio:checked')。数据('location');
window.location.href=位置;
返回false;
});
....
您可以使用html表单,也可以不使用,这并不重要,还有其他一些方法。
最简单的方法是只使用标签。它的href属性设置为相对url,但您希望走更长的路
<a href="{% url 'fullscan' %}">Full Scan</a>
<a href="{% url 'quickscan' %}">QuickScan</a>
或者您可以将onclick设置为收音机输入本身!:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="sname"
id="thisId1" autocomplete="off" checked
onclick="javascript:window.location.href({% url 'fullscan' %});">
</label>
<label class="btn btn-primary">
<input type="radio" name="sname"
id="thisId2" autocomplete="off"
onclick="javascript:window.location.href=`{% url 'quickscan' %}`; return false;">
</label>
</div>
甚至可以使用第三个按钮来更改位置
<div class="btn-group scan-selector" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="sname"
id="thisId1" autocomplete="off" checked
data-location="{% url 'fullscan' %}">
</label>
<label class="btn btn-primary">
<input type="radio" name="sname"
id="thisId2" autocomplete="off"
data-location="{% url 'quickscan' %}">
</label>
</div>
<button type="button" id="startScanBtn"
>Start Now!</button>
<script>
$("#startScanBtn").on('click', function(){
var location = $('.scan-selecor :radio:checked').data('location');
window.location.href=location;
return false;
});
</script>
现在就开始!
$(“#开始扫描”)。在('单击',函数()上{
变量位置=$('.scan selecor:radio:checked')。数据('location');
window.location.href=位置;
返回false;
});
你所说的无线电值变量是什么意思?您应该在views.pyradio\u value=request.GET.GET('ext\u com')
中传递fullscan
或quickscan
,然后像通过render一样将其传递到base.htmlonclick=“location.href=”{%url'{{radio\u value}}'%}'
中,让我们看看您实际尝试了什么。而且,变量来自无线电值变量
?href='{%url radio_value%}'
第三个代码似乎就是我想要的。但这会有什么不同吗?1.首先添加此
2。是否$('.scan selector:radio:checked')
此扫描选择器指的是位于
的扫描选择器?3.执行此操作后,将显示http://127.0.0.1:8000/undefined
/undefinedI已使用解决方案(我尝试的命名解决方案)编辑了我的问题。请参考您的代码片段,您能告诉我哪里错了吗。我从来没有尝试过jQuery,所以..对于评论,我认为在第一次出现:
之前,缺少了一个空格,但是第二次没有空格。但是我说,没有必要使用jQuery。javascript在很多方面都非常好