问题描述
表单中有这个隐藏字段
< input type =hiddenname =dir_typevalue =tshirt >
网址参数
/ dir?type = tshirt
/ dir?type = books
/ dir?type = posters
等等。
现在我硬编码 value =tshirts但我如何获取相关页面的参数?
我找到了几个页面完美地工作,但我决定不使用模板来解决问题。对于有类似问题的任何人,将url参数传递给这样的表单效果很好:
< form name = submit_formaction =/ directorysubmithandlermethod =postonSubmit =return validate_form()>
title:< input type =textname =titlesize = 50>< br />>
url:< input type =textname =urlsize = 50>< br />
< input type =hiddenname =dir_typevalue =%s)>
< input type =submitvalue =submit>
< / form>%self.request.get(type))
将类型值传递给视图:
class Directory(webapp.RequestHandler):
def get(self):
....
merchandise_type = self .request.get(type,)
items = Item.all()
items.filter(type =,merchandise_type)
path = os.path.join( os.path.dirname(__ file__),'dir_details.html')
self.response.out.write(template.render(path,{'type':merchandise_type}))
b。将类型值添加到隐藏字段:
< input type =hiddenname =dir_typevalue ={{type}}>
c。取得 dir_type value在你的发布处理器中:
class DirectorySubmitHandler(webap p.RequestHandler):
def post(self):
user = users.get_current_user()
merchandise_type = self.request.get(dir_type,)
dir_type = merchandise_type
if user:
item = Item()
item.title = self.request.get(title)
item.url = self.request.get( url)
item.type = self.request.get(dir_type)
item.user_who_liked_this_item = user
item.put()
self.redirect(/ dir?type =%s%
self.request.get(dir_type))
else:
self.redirect(users.create_login_url(self.request.uri))
The form has this hidden field
<input type="hidden" name="dir_type" value="tshirt">
url parameters are
/dir?type=tshirt /dir?type=books /dir?type=posters
and so on.
Now I hard coded value="tshirts" but how do I get parameter for the relevant page?
I found several pages like this dealing with similar topics but I did not understand how this is done.
Thanks for your help.
UPDATE
The answer by systempuntoout works perfectly but I decided to solve the problem without using templates. And for anyone who has a similar question, passing the url parameter to the form like this works well:
<form name="submit_form" action="/directorysubmithandler" method="post" onSubmit="return validate_form()"> title: <input type="text" name="title" size=50><br /> url: <input type="text" name="url" size=50><br /> <input type="hidden" name="dir_type" value="%s")> <input type="submit" value="submit"> </form>""" % self.request.get("type"))
a. pass the type value to the view:
class Directory(webapp.RequestHandler): def get(self): .... merchandise_type = self.request.get("type", "") items = Item.all() items.filter("type =", merchandise_type) path = os.path.join(os.path.dirname(__file__), 'dir_details.html') self.response.out.write(template.render(path,{'type':merchandise_type}))
b. add the type value to the hidden field:
<input type="hidden" name="dir_type" value="{{ type }}">
c. get the dir_type value in your post handler:
class DirectorySubmitHandler(webapp.RequestHandler): def post(self): user = users.get_current_user() merchandise_type = self.request.get("dir_type", "") dir_type = merchandise_type if user: item = Item() item.title = self.request.get("title") item.url = self.request.get("url") item.type = self.request.get("dir_type") item.user_who_liked_this_item = user item.put() self.redirect("/dir?type=%s" % self.request.get("dir_type")) else: self.redirect(users.create_login_url(self.request.uri))
这篇关于我如何传递url参数来形成值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!