I use a form in template,and I want to redirect a friendly url when user fill the form
I search many issue in stackoverflow,I think my problem like this issue: Django form redirect,
but I can’t understand the template tag,so I can’t solve my problem.
I write the form:
<form method="GET" action="/stock/search">
search:<input class="search" name="search" type="text" value="" id="serach" >
<input type="submit" value="Go"/>
</form>
and write the urls:
url(r'^(?P<number>\w+)/$', 'stock.views.stocknumber'),
url(r'^search/$', 'stock.views.search'),
and the views:
def stocknumber(request, number):
"""
stock info
"""
stock = Stock.objects.filter(number="%s"%number)
stock_number = Stock.objects.filter(number="%s"%number)
stock_reportinfo = Reportinfo.objects.filter(number="%s"%number)
stock_blockinfo = Blockinfo.objects.filter(number="%s"%number)
stock_stockinfo = Stockinfo.objects.filter(number="%s"%number)
data = Stockhq.objects.filter(number="%s"%number).values('timeStamps','openData','highData','lowData', 'closeData', 'volData').reverse()
datalist=[ ]
for item in data:
d =[item['timeStamps'].toordinal(),item['openData'],item['highData'],item['lowData'],item['closeData'],item['volData']]
datalist.append(d)
hisdata = datalist
return render_to_response(
'stock/stock.html',
{"stock_number": stock_number,
"stock_reportinfo": stock_reportinfo,
"stock_blockinfo": stock_blockinfo,
"stock_stockinfo": stock_stockinfo,
"hisdata":simplejson.dumps(hisdata) ,
},
context_instance=RequestContext(request))
def search(request):
return HttpResponseRedirect('/stock/%s/'%request.GET['search'])
and now I hope the user input the stock number and redriect to the friendly url like:http://…./stock/YHOO
and this url is get the stock info to render template,is this idea is right?
if it is ,what is the correct code ?
I don’t think that question is really similar to your question, if I’m understanding correctly. Because that one already predetermines the action of the form, the redirected URL doesn’t seem to depend on the user input. So I’m not entirely sure what your question is.
Are you implementing a simple search that, when a user inputs the stock’s number (I’m assuming you consider “YHOO” a number? Is the “number” input something like “YHOO” or is it actually a number?), the stock with the number (so “YHOO” stock) is displayed with all of its information on a new page with the URL “http://…/stock/YHOO”?
In any case, here are some observations.
Is there a reason why you have both when you don’t use the first one? In addition, are numbers unique? If so, you can just do Stock.objects.get(number=number), because:
single record to match that.
passed in, so you don’t need to do “%s” % number.
You can also try the following instead of using the actual URL, to make it more Django-like.