Jaquacky
Jaquacky

Reputation: 561

Submit Button Confusion and Request being sent Twice (Using Flask)

I'm pretty much trying to create a web app that takes 2 svn urls and does something with them.

The code for my form is simple, I'm also using WTForms

class SVN_Path(Form):
svn_url=StringField('SVN_Path',[validators.URL()])

I'm trying to create 2 forms with 2 submit buttons that submit the 2 urls individually so my test3.html looks like this:

<form action="" method="post" name="SVNPath1">
        {{form1.hidden_tag()}}
        <p>
            SVN Directory:
            {{form1.svn_url(size=50)}}
            <input type="submit" value="Update">
            <br>
            {% for 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>
                {% for error in form2.svn_url.errors %}
                    <span style="color: red;">[{{error}}]</span>
                {% endfor %}
            </p>
</form>

MY FIRST QUESTION is how do I know which submit button was clicked so I can run the proper function on the corresponding svn url. I have tried doing something like

if request.form1['submit'] == 'Update':
if request.form2['submit'] == 'Update':

but that does not work at all. I'm new to web dev in general and flask so a detailed explanation would be helpful.

SECONDLY, since submits weren't working properly I also tried an alternative to keep my work moving so in my .py file I 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 is a function that creates a directory in the local /tmp using timestamps of the local time.

Whenever I go the webpage it creates a directory, lets call it dir1, since its calling CreateDir. Thats what I want, but when I click submit on the form it creates another directory dir2 in the tmp folder which is NOT what I want since I want everything to being the same dir1 directory.

In addition when I put a url in one of the forms and click submit, it automatically puts it the same value in the 2nd form as well.

Sorry if this is really long and possibly confusing, but any help is appreciated.

Upvotes: 0

Views: 1586

Answers (1)

junnytony
junnytony

Reputation: 3515

:) Let's see if we can clarify this a little.

To your first question:

As @dim suggested in his comment, You have a few options:

  • You can submit your form to separate unique urls. That way you know which form was submitted
  • You can create two similar but different Form classes (the fields will need different names like prev_svn_url and cur_svn_url). This way in your view function, you instantiate two different forms and you'll know which form was submitted based on form.validate_on_submit()
  • The third option would be to add a name attribute to your submit button and then change the value attributes to something like 'Update Previous' and 'Update Current'. This way in your view function you can check the value of request.data[<submit button name>] to determine if 'Update Previous' was pressed or 'Update Current'.

To your second question:

Multiple directories are being created because you're calling createDir() each time the page is loaded to show the forms and when the forms get posted. In order to create just once, you'll need some kind of logic to determine that the directory was not previously created before calling createDir()

In addition: Since both forms are from the same SVN_Path class, they read post data exactly the same way, that's why whatever you type in form 1 appears in form 2.

Now for my 2 cents:

I assume you're trying to write some kind of application that takes two SVN urls as input, creates a folder and does something with those URLs in that folder. If this is the case, the way you are currently going about it is inefficient and won't work well. You can achieve this with just one form class having 2 svn_url fields (with different names of course) and then handling all of that in one post.

EDIT: The job of the submit button is to tell the browser that you're ready to send the data on the form to the server. In this case you should only need one submit button (SubmitFiled => when rendered). Clicking that one submit button will send data from both input fields to your view function.

Your form should look something like:

class SVN_Path(Form):
    prev_svn_url=StringField('Previous SVN_Path',[validators.URL()])
    new_svn_url=StringField('New SVN_Path',[validators.URL()])

and your view function:

def test():
form = SVN_Path()
if request.method == "POST":
    if form.validate_on_submit():
        basepath = createDir() # Only create dir when everything 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)

Upvotes: 1

Related Questions