python 2.7 - Submit Button Confusion and Request being sent Twice (Using Flask) -
i'm pretty trying create web app takes 2 svn urls , them.
the code form simple, i'm using wtforms
class svn_path(form): svn_url=stringfield('svn_path',[validators.url()])
i'm trying create 2 forms 2 submit buttons submit 2 urls individually test3.html
looks this:
<form action="" method="post" name="svnpath1"> {{form1.hidden_tag()}} <p> svn directory: {{form1.svn_url(size=50)}} <input type="submit" value="update"> <br> {% error in form1.svn_url.errors %} <span style="color: red;">[{{error}}]</span> {% endfor %} </p> </form> <form action="" method="post" name="svnpath2"> {{form2.hidden_tag()}} <p> svn directory: {{form2.svn_url(size=50)}} <input type="submit" value="update"> <br> {% error in form2.svn_url.errors %} <span style="color: red;">[{{error}}]</span> {% endfor %} </p> </form>
my first question is how know submit button clicked can run proper function on corresponding svn url. have tried doing
if request.form1['submit'] == 'update': if request.form2['submit'] == 'update':
but not work @ all. i'm new web dev in general , flask detailed explanation helpful.
secondly, since submits weren't working tried alternative keep work moving in .py file have
@app.route('/test3', methods=['get','post']) def test3(): basepath=createdir() form1=svn_path() form2=svn_path() if request.method=="post": if form1.validate_on_submit(): svn_url = form1.svn_url.data prev_pdf=pdf_list(svn_url,basepath,'prev') #some function if form2.validate_on_submit(): svn_url2 = form2.svn_url.data new_pdf=pdf_list(svn_url,basepath,'new') #some function return render_template('test3.html', form1=form1, form2=form2)
createdir function creates directory in local /tmp using timestamps of local time.
whenever go webpage creates directory, lets call dir1, since calling createdir. thats want, when click submit on form creates directory dir2 in tmp folder not want since want being same dir1 directory.
in addition when put url in 1 of forms , click submit, automatically puts same value in 2nd form well.
sorry if long , possibly confusing, appreciated.
:) let's see if can clarify little.
to first question:
as @dim suggested in comment, have few options:
- you can submit form separate unique urls. way know form submitted
- you can create 2 similar different form classes (the fields need different names
prev_svn_url
,cur_svn_url
). way in view function, instantiate 2 different forms , you'll know form submitted based onform.validate_on_submit()
- the third option add
name
attribute submit button , changevalue
attributes 'update previous' , 'update current'. way in view function can check value ofrequest.data[<submit button name>]
determine if 'update previous' pressed or 'update current'.
to second question:
multiple directories being created because you're calling createdir()
each time page loaded show forms , when forms posted. in order create once, you'll need kind of logic determine directory not created before calling createdir()
in addition: since both forms same svn_path class, read post data same way, that's why whatever type in form 1 appears in form 2.
now 2 cents:
i assume you're trying write kind of application takes 2 svn urls input, creates folder , urls in folder. if case, way going inefficient , won't work well. can achieve 1 form class having 2 svn_url fields (with different names of course) , handling of in 1 post.
edit: job of submit button tell browser you're ready send data on form server. in case should need 1 submit button (submitfiled => when rendered). clicking 1 submit button send data both input fields view function.
your form should like:
class svn_path(form): prev_svn_url=stringfield('previous svn_path',[validators.url()]) new_svn_url=stringfield('new svn_path',[validators.url()])
and view function:
def test(): form = svn_path() if request.method == "post": if form.validate_on_submit(): basepath = createdir() # create dir when validates prev_svn_url = form.prev_svn_url.data new_svn_url = form.new_svn_url.data prev_pdf = pdf_list(prev_svn_url, basepath, 'prev') new_pdf = pdf_list(new_svn_url, basepath, 'new') ... return render_template('test3.html', form1=form1, form2=form2)
Comments
Post a Comment