传递值<a> to decorator

时间:2018-09-09 19:09:44

标签: python html flask href

Im just learning Python, I' using Flask and I have a problem: I try to pass values from one page to my decorator, but this value comes from and this does not have name property. This is my html:

<table border="4">
<tr>
    <td>ID</td>
    <td>Name</td>
    <td>Autor</td>
    <td>Year</td>
    <td>Genre</td>
    <td>Excluir</td>
    <td>Actualizar</td>
</tr>
{% for album in listalbumes %}
<tr>
    <td>{{ album._id }}</td>
    <td>{{ album.name }}</td>
    <td><a href="/artist/{{ album.autor }}" id="autor" >{{ album.autor }}</a></td>
    <td>{{ album.year }}</td>
    <td>{{ album.name_genre }}</td>
    <td><a href="/excluir/{{ album._id }}">X</a></td>
    <td><a href="/actualizar/{{ album._id }}">Actualizar</a></td>
</tr>
{% endfor %}

I try to pass {{ album.autor }} with the id, but I can't. And this is my py code:

@app.route('/artist/<string:autor>',  methods=['GET', 'POST'])
def artista(autor):
    autor = request.form.get("autor")
    alb = db.session.query(Album._id, Album.name, Album.autor, Album.year, 
    Album.media, Album.continent).filter(Album.autor == autor).all()
    if request.method == "GET":

        for g in alb:
            print("Album {} skill points.".format(g)) # testing db values, It works if I set manually values

        if alb:
            return render_template("artist.html", alb=alb)

Please, I hope anyone can help me best Regards

2 个答案:

答案 0 :(得分:0)

在您的视图/模板中,您尝试遍历listalbumes,但我看不到任何地方将其传递。

因此,我认为您需要通过以下方式传递相册列表:

@app.route('/artist/<string:autor>',  methods=['GET', 'POST'])
def artista(autor):
    autor = request.form.get("autor")

    if request.method == "GET": # Handle GET request
        alb = db.session.query(Album._id, Album.name, Album.autor, Album.year,
                               Album.media, Album.continent).filter(Album.autor=autor)

        if alb.exists(): # Handle if matching album list exists
            return render_template("artist.html", {"listalbumes": alb})
        else: # Handle if matching albumlist is not found
            return render_template("artist.html", {"listalbumes": []})

    else: # Handle POST request
        Album.create(autor)
        return Response("Created", status=201)

答案 1 :(得分:0)

我使用带有参数的url的按钮发送值:

<table border="4">
<tr>
    <td>ID</td>
    <td>Name</td>
    <td>Autor</td>
    <td>Year</td>
    <td>Genre</td>
    <td>Excluir</td>
    <td>Actualizar</td>
</tr>
{% for album in listalbumes %}
<tr>
    <td>{{ album._id }}</td>
    <td>{{ album.name }}</td>
    <!-- <td><a href="/artist/{{ album.autor }}"id="autor" >{{ album.autor }}</a></td> -->  <!-- esta vaina no funciona.... no se por que -->
    <!-- para que funcione enviamos los datos -->
    <td><input type="submit" value="{{ album.autor }}" name="autor" onclick="window.location.href='/artist/artist?autor={{ album.autor }}'" /></td>
    <td>{{ album.year }}</td>
    <td>{{ album.name_genre }}</td>
    <td><a href="/excluir/{{ album._id }}">X</a></td>
    <td><a href="/actualizar/{{ album._id }}">Actualizar</a></td>
</tr>
{% endfor %}

在我的代码中,我通过“ request.args.get”获取了值

@app.route('/artist/<string:autor>',  methods=['GET', 'POST'])
def artista(autor):
if request.method == "GET":
    print('Im in GET')
    # autor = request.form.get("autor")
    autor = request.args.get('autor')
    print ("Autor=> ", autor)

感谢您的帮助!         alb = db.session.query(Album._id,Album.name,Album.autor,Album.year,Album.media,Album.continent).filter(Album.autor == autor).all()         对于g in alb:             print(“相册{}技能点。”。format(g))#测试数据库值

    return render_template("artist.html", autor=autor, alb=alb)