1

I'm writing a program in flask. In one of the pages (/search), the user will have a field to enter a string and click a search button. The result page that follows works all right, which consists of an html page with several images on it.

Now i want to add a button next to each image that, once clicked, will add an item to my mongoDB database. So, 2 problems:
1) The buttons i managed to create so far will demand a "return redirect(page)". I don't want that. The user must be allowed to click several buttons in sequence, instead of having to perform a new search.

2) i built my own mechanism for rendering the result.html page. According to the search results, my code opens result.html and writes (appends) the urls on it.

@app.route('/add', methods = ['POST'])
def add():
    print "hi"
    return None ##Python won't accept 'return None' here =(

[...]

w=open("result.html", "a")

[...]

for k in cursor:
    try:
           w.write(k['url'] + "'> <form action='/add' method='post'> \
  <input type='submit' value='Add'></input></form>")

How can i make the button tell the /add view function know the url for each image? I suppose there is something i should replace in this last block of code.

David Spira
  • 153
  • 2
  • 16
  • 1
    I was trying to do the same thing and [this link](http://stackoverflow.com/questions/24295426/python-flask-intentional-empty-response) helped a lot. Best of luck. – cheevahagadog Apr 27 '16 at 15:43
  • Big warning: This is not thread safe `w=open("result.html", "a")`. Jinja templating is shipped with Flask for these types of things. – wgwz Apr 27 '16 at 16:11

0 Answers0