Forums

Problems using JSON database, python (Flask) and html.

I am trying to make a website where I can create Activities where you can register yourself (to make an appointment). I already finished the system that creates them, but I'm trying to make a page where a visitor can fill in his/her name (using a list, as which is part of the activity information in the JSON file). I already made this code:

@app.route('/deelnemen/<act>', methods=['POST','GET'])
def AddPcp(act):
    if request.method=='GET':
        return render_template('Main/Deelname.html', Data=Actdata, act=act)
    if request.method=='POST':
        visitors = []
        if request.form['visitor']:
            visitors.append(request.form['visitor'])
        Actdata.data[act]={
            "visitors":visitors
        }
        Actdata.save()
        return render_template("Main/Startpage.html", Data=Actdata)

Hereby Data=Actdata (json file). The website already creates the list visitors=[] while adding an activity. This code worked until I realised that the earlier created variables (for the activities, such as time and day) were removed and the list can't hold more than 2 Items. My list is called visitor, and I really want to keep the other data and make lists which can hold more than 2 items. I tried to fix it, but nothing helped.

[formatted by admin]

I'm not quite sure what are you trying to do -- do you mean that Actdata.data[act]["visitors"] is being overwritten by the form? What do you mean by saying that the list can hold only 2 items? What are the earlier created variables?

I changed it a little bit to this:

@app.route('/deelnemen/<act>', methods=['POST','GET'])
def Addvisitor(act):
    if request.method=='POST':
        visitors = []
        visitors.append(request.form['visitor'])
        return redirect(url_for('gelukt'))
    if request.method == "GET":
        return render_template("Main/Deelname.html", Data=Actdata)

The list is created by another app.route, and the list is visible in the JSON file element. When I try to add an item in the list visitors, it doesn't do anything. Here is my HTML code:

{% extends "normal_layout.html" %}
{% set title = "" %}
{% block menu %}
{{ menu("Back","/") }}
{% endblock %}
{% block content %}
    {% call card() %}
        <h1>Deelnemen</h1>
    {% endcall %}
    {% call card() %}
<form method='POST'>
          <input type='text' placeholder='Naam'   name='visitor'   value='' required><br>
            <input type="submit" value="Save" style='margin-top:0px;'>
    {% endcall %}
{% endblock %}

I reviewed this code by someone with a little bit more experience, but we both don't understand that it doesn't add the item (visitor) to the list.

Gelukt is the def of the "Succesful" page. Main/Deelname.html is the page where you can fill in your name.

In your most recent code, you are adding the visitor to the list, you're just not doing anything with it after that. In your original code, at each request, you took and empty list, appended 1 item to it, then overwrote the list that was in ActData with the 1 item list and then saved ActData. So the saved ActData would always have one entry in it and that entry would be the most recent visitor.

If you want to update the visitors list so that it keeps getting more entries, you need to get the existing one from ActData and append to that, not to a new empty list.

.

It can't even hold 1 item. So how can I change the code to make it work?

If you define the variable visitors outside your view function, then it will be accessible from inside, and you won't get a new one created each time that the view is called.

However, this won't work well in the long term. Every time you reload your website, the visitors variable will be cleared. And if you ever upgrade to a paid account, you will have multiple instances of your script running in parallel, in order to handle more traffic, so they'll all have their own copies of the visitors list, so things will be inconsistent from request to request.

The normal way to write a website would be to use a database to store the data that you want to persist over time; you might find our blog post useful.