0

I will try my best to be as concise as possible.

In my back end, I have a list of dictionaries saved to a variable. Each dictionary represents a post from reddit and includes the score, url, and title.

Respectively, the template will loop through this list and then return the values of each of these keys to the user like so:

<table>
    <tr>

    {% for x in data[0:5] %}
    <td>
      {{ x['score'] }}

      <a href="{{x['url']}}"> {{ x['title'] }} </a>

      <br>

      <a href='/add_to_favorites'> Add To Favorites </a>


    </td>
    {% endfor %}

  </tr>

</table>

As you can see, there's an tag which is linked to a function on my utils.py that is attempting to save the respective dictionary to the database (I have a model that represents the url, title, and score).

I feel as though my template is not representing the dictionary in the correct way, for my link to include the html as when it is pressed I receive a 404 error (though I have this route already defined in views.py - '/add_to_favorites' which calls my 'save_post' function).

def save_post():
    data = get_info()
    for post in data:
        fav= Favorite(title=post.get('title'), url=post.get('url'), score=post.get('score'), user_id=current_user.id)
        db.session.add(fav)
        db.session.commit()
    return redirect(url_for('favorites'))

and:

@app.route('/add_to_favorites')
@login_required
def add_to_favorites():
    return save_post()

Am i going about this the wrong way? How can i make sure that the link/button is associated with only the html of the that it is included in?

Just need some guidance into the right direction here, not necessarily the code to fix it. Thank you

Steve Novosel
  • 45
  • 1
  • 6
  • you should really separate the backend development from the frontend development. test the backend by running the server and curl-ing requests into it. then, once it works, you can fix the html-side. test one thing at a time. – FuzzyAmi Mar 05 '17 at 13:23
  • you are right. that will probably help me a lot with this application – Steve Novosel Mar 05 '17 at 13:30
  • what do you mean by curl-ing requests to the server? this particular function relies on user input and I just don't know how to move from the user input to the alteration of the information in the back end. – Steve Novosel Mar 05 '17 at 21:07
  • cURL (commonly pronounced 'kurl') is a unix utility that can be used to generate HTTP (and other) requests from the command line. its also available for windows. Sending a POST request with a json payload (which is what you need) is trivial: http://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest – FuzzyAmi Mar 06 '17 at 06:20

0 Answers0