我必须根据用户选择使用新图像更新同一页面
View .py
def search_form(request,xid):
print xid
if request.GET.get('search'):
search = request.GET.get('search')
print "in search"
print search
xid = search
return render(request,'search_form.html',{'xid':xid})
else:
return render(request,'search_form.html',{'xid':xid})
def temp(request,xid):
""" forming chart image using matplotlib"""
urls.py
urlpatterns = patterns('',
url(r'^temp/(?P<xid>\d{4})/$', views.temp, name='temp'),
url(r'^search_form/(?P<xid>\d{4})/$', views.search_form, name='search_form'),)
template search_form.html
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="get">
<select name=search>
<option value="1234">1234</option>
<option value="7426">7426</option>
</select>
<input type="submit" value="Search">
</form>
<div id="content">
{% block content %}
<img src='/temp/{{ xid }}'>
{% endblock %}
</div>
</body>
</html>
我的问题是,当我从下拉列表中选择选项时,它正在寻找像这样的网址 得到 下面(这里我正在查看 1234 的图像),并在从下拉列表中选择 7426 后尝试获取新图像,但 url 形成如下,并且页面未更新,如下 url 形成。
/search_form/1234/?search=7426
但我需要它应该形成类似
/search_form/7426
我在这里做错了什么吗?最好的处理方法是什么,或者我应该更改 url 模式吗?
最佳答案
您的form
没有action
属性,因此GET将定向到当前url,即/search_form/1234/
。如果您想将其定向到另一个网址,请添加 action
属性。例如
<form method="get" action="/search_form/">
如果选择 7426
,这将产生 URL /search_form/?search=7426
。您还需要修改 View :
def search_form(request, xid=None):
# Same here
如果您不想硬编码 /search_form/
,您可以在模式中添加另一个命名网址,例如
url(r'^search_form/$', views.search_form, name='base_search_form')
还有
<form method="get" action="{% url 'base_search_form' %}">
关于javascript - 无法根据下拉选项更新同一页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23786075/