2016-11-15 2 views
-1

Ok, also habe ich zwei HTML-Dateien. base.html und search_results.htmlTasten auf zweite HTML-Datei funktioniert nicht für Flask

base.html

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Title</title> 
 
    <style> 
 
    body { 
 
     font-family: sans-serif; 
 
     background-image: url('/static/Flat-Color-Gradient-Triangles.png'); 
 
     width: 430px; 
 
     height: 763px; 
 
     align-items: center; 
 
    } 
 

 
    h1 { 
 
     font-family: "Century Gothic", CenturyGothic, AppleGothic, sans-serif; 
 
    \t font-size: 45px; 
 
     font-color: #333333; 
 
    \t font-weight: bold; 
 
     margin: 0; 
 
     padding: 3px; 
 
     text-align: center; 
 
    } 
 

 
    h2 { 
 
     font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, AppleGothic, sans-serif; 
 
    \t font-size: 15px; 
 
    \t font-weight: bold; 
 
     margin: 0; 
 
     padding: 3px; 
 
     border: 1.5px 
 
    } 
 

 
    h3 { 
 
    \t font-family: Candara, Calibri, Segoe, "Segoe UI", Optima, Arial, sans-serif; 
 
    \t font-size: 14px; 
 
    \t font-weight: 100; 
 
     margin: 0; 
 
     border-top: 1px; 
 
     border-bottom: 1px; 
 
     padding: 3px; 
 
    } 
 

 
    .explicit{ 
 
     color: rgb(200, 0, 0); 
 
    } 
 

 
    .rank{ 
 
     font-weight: bold; 
 
    } 
 

 
    table { 
 
     border-collapse: collapse; 
 
     padding: 4px; 
 
    } 
 

 
    table, th, td { 
 
     padding: 5px; 
 
     border-bottom: 1px solid black; 
 
     color: white; 
 
     text-align: center; 
 
    } 
 

 
    th { 
 
     background-color: #121f1f; 
 
     border-left: #121f1f; 
 
     border-right:#121f1f; 
 
    } 
 

 
    td { 
 
     background-color: #253f3f; 
 
     border-left: #253f3f; 
 
     border-right: #253f3f; 
 

 
    } 
 

 
    input[type='text'] { 
 
     width: 100%; 
 
     padding: 6px 10px; 
 
     margin: 6px 0; 
 
     box-sizing: border-box; 
 
     border: 2px solid #ccc; 
 
     font-size: 14px; 
 
     border-radius: 10px; 
 
    } 
 

 
    input[type='text']:focus { 
 
     border: 3px solid red; 
 
    } 
 

 
    input[type = 'submit'] { 
 
     background-color: #4CAF50; 
 
     border: none; 
 
     color: white; 
 
     padding: 16px 32px; 
 
     text-decoration: none; 
 
     margin: 4px 2px; 
 
     width: 100%; 
 
    } 
 

 
    input[value="Up"] { 
 
     background-color: #4CAF50; 
 
     border: none; 
 
     color: white; 
 
     padding: 16px 32px; 
 
     text-decoration: none; 
 
     margin: 4px 2px; 
 
    } 
 

 
    input[value="Down"] { 
 
     background-color: rgb(200,0,0); 
 
     border: none; 
 
     color: white; 
 
     padding: 16px 32px; 
 
     text-decoration: none; 
 
     margin: 4px 2px; 
 
    } 
 

 
    input[value="Add"] { 
 
     border: 3px solid #4CAF50; 
 
     color: white; 
 
     padding: 16px 32px; 
 
     text-decoration: none; 
 
     margin: 4px 2px; 
 
    } 
 

 
    div.content { 
 
     width: 430px; 
 
     align-items: center; 
 
    } 
 

 
</style> 
 
</head> 
 
<body> 
 
<div class="content"> 
 
<h1>Public Playlist</h1> 
 

 

 
<form action = '/search_results' method="POST"> 
 
    <input placeholder="Artist:" type = 'text' name='artist'> </input> 
 
    <input placeholder="Album:" type = 'text' name='album'> </input> 
 
    <input placeholder="Track:" type = 'text' name='track'> </input> 
 
    <input type="submit" value="Submit!" id="search"/> 
 
</form> 
 

 
<table > 
 
    <tr> 
 
     <th><h2>Rank</h2></th> 
 
     <th><h2>Title</h2> 
 
      <h2>Author|Album</h2></th> 
 
     <th><h2>Score</h2></th> 
 
     <th><h2>Vote</h2></th> 
 
    </tr> 
 
    <tr> 
 
     <td><h3 type="bold">1</h3></td> 
 
     <td><h3>{{name}}</h3> 
 
      <h3>{{artist}}:{{album}}</h3></td> 
 
     <td><h3>{{ score }}</h3></td> 
 
     <form action="/vote" method="POST"> 
 
      <td> 
 
       <input type="button" name="vote" value="Up" id="up"> 
 
       <input type="button" name= "vote" value="Down" id="down"> 
 
      </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <td><h3 type="bold">2</h3></td> 
 
     <td><h3>{{name1}}</h3> 
 
      <h3>{{artist1}}:{{album1}}</h3></td> 
 
     <td><h3>{{ score1 }}</h3></td> 
 
     <form action="/" method="POST"> 
 
      <td> 
 
       <input type="button" name="vote1" value="Up" id="up"> 
 
       <input type="button" name= "vote1" value="Down" id="down"> 
 
      </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <td><h3 type="bold">3</h3></td> 
 
     <td><h3>{{name2}}</h3> 
 
      <h3>{{artist2}}:{{album2}}</h3></td> 
 
     <td><h3>{{ score2 }}</h3></td> 
 
     <form action="/vote" method="POST"> 
 
      <td> 
 
       <input type="button" name="vote2" value="Up" id="up"> 
 
       <input type="button" name= "vote2" value="Down" id="down"> 
 
      </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <td><h3 type="bold">4</h3></td> 
 
     <td><h3>{{name3}}</h3> 
 
      <h3>{{artist3}}:{{album3}}</h3></td> 
 
     <td><h3>{{ score3 }}</h3></td> 
 
     <form action="/vote" method="POST"> 
 
      <td> 
 
       <input type="button" name="vote3" value="Up" id="up"> 
 
       <input type="button" name= "vote3" value="Down" id="down"> 
 
      </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <td><h3 type="bold">5</h3></td> 
 
     <td><h3>{{name4}}</h3> 
 
      <h3>{{artist4}}:{{album4}}</h3></td> 
 
     <td><h3>{{ score4 }}</h3></td> 
 
     <form action="/vote" method="POST"> 
 
      <td> 
 
       <input type="button" name="vote4" value="Up" id="up"> 
 
       <input type="button" name= "vote4" value="Down" id="down"> 
 
      </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <td><h3 type="bold">6</h3></td> 
 
     <td><h3>{{name5}}</h3> 
 
      <h3>{{artist5}}:{{album5}}</h3></td> 
 
     <td><h3>{{ score5 }}</h3></td> 
 
     <form action="/vote" method="POST"> 
 
      <td> 
 
       <input type="button" name="vote5" value="Up" id="up"> 
 
       <input type="button" name= "vote5" value="Down" id="down"> 
 
      </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <td><h3 type="bold">7</h3></td> 
 
     <td><h3>{{name6}}</h3> 
 
      <h3>{{artist6}}:{{album6}}</h3></td> 
 
     <td><h3>{{ score6 }}</h3></td> 
 
     <form action="/vote" method="POST"> 
 
      <td> 
 
       <input type="button" name="vote6" value="Up" id="up"> 
 
       <input type="button" name= "vote6" value="Down" id="down"> 
 
      </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <td><h3 type="bold">8</h3></td> 
 
     <td><h3>{{name7}}</h3> 
 
      <h3>{{artist7}}:{{album7}}</h3></td> 
 
     <td><h3>{{ score7 }}</h3></td> 
 
     <form action="/vote" method="POST"> 
 
      <td> 
 
       <input type="button" name="vote7" value="Up" id="up"> 
 
       <input type="button" name= "vote7" value="Down" id="down"> 
 
      </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <td><h3 type="bold">9</h3></td> 
 
     <td><h3>{{name8}}</h3> 
 
      <h3>{{artist8}}:{{album8}}</h3></td> 
 
     <td><h3>{{ score8 }}</h3></td> 
 
     <form action="/vote" method="POST"> 
 
      <td> 
 
       <input type="button" name="vote8" value="Up" id="up"> 
 
       <input type="button" name= "vote8" value="Down" id="down"> 
 
      </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <td><h3 type="bold">10</h3></td> 
 
     <td><h3>{{name9}}</h3> 
 
      <h3>{{artist9}}:{{album9}}</h3></td> 
 
     <td><h3>{{ score9 }}</h3></td> 
 
     <form action="/vote" method="POST"> 
 
      <td> 
 
       <input type="button" name="vote9" value="Up" id="up"> 
 
       <input type="button" name= "vote9" value="Down" id="down"> 
 
      </td> 
 
     </form> 
 
    </tr> 
 
</table> 
 
<script> 
 
setTimeout(function() {location.reload() },60000); 
 
</script> 
 
</div> 
 
</body> 
 
</html>

search_results.html

<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Title</title> 
 
    <style> 
 
    body { 
 
     font-family: sans-serif; 
 
     background-image: url('/static/Flat-Color-Gradient-Triangles.png'); 
 
     width: 640px; 
 
     height: 1136px; 
 
    } 
 
    h1 { 
 
     font-family: "Century Gothic", CenturyGothic, AppleGothic, sans-serif; 
 
     font-size: 45px; 
 
     font-color: #333333; 
 
     font-weight: bold; 
 
     margin: 0; 
 
     padding: 3px; 
 
     text-align: center; 
 
    } 
 
    h2 { 
 
     font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, AppleGothic, sans-serif; 
 
     font-size: 15px; 
 
     font-weight: bold; 
 
     margin: 0; 
 
     padding: 3px; 
 
     border: 1.5px 
 
    } 
 
    h3 { 
 
     font-family: Candara, Calibri, Segoe, "Segoe UI", Optima, Arial, sans-serif; 
 
     font-size: 14px; 
 
     font-weight: 100; 
 
     margin: 0; 
 
     border-top: 1px; 
 
     border-bottom: 1px; 
 
     padding: 3px; 
 
    } 
 
    .explicit { 
 
     color: rgb(200, 0, 0); 
 
    } 
 
    .rank { 
 
     font-weight: bold; 
 
    } 
 
    table { 
 
     border-collapse: collapse; 
 
     padding: 4px; 
 
    } 
 
    table, 
 
    th, 
 
    td { 
 
     padding: 5px; 
 
     border-bottom: 1px solid black; 
 
     color: white; 
 
     text-align: center; 
 
    } 
 
    th { 
 
     background-color: #121f1f; 
 
     border-left: #121f1f; 
 
     border-right: #121f1f; 
 
    } 
 
    td { 
 
     background-color: #253f3f; 
 
     border-left: #253f3f; 
 
     border-right: #253f3f; 
 
    } 
 
    input[type='text'] { 
 
     width: 100%; 
 
     padding: 6px 10px; 
 
     margin: 6px 0; 
 
     box-sizing: border-box; 
 
     border: 2px solid #ccc; 
 
     font-size: 14px; 
 
     border-radius: 10px; 
 
    } 
 
    input[type='text']:focus { 
 
     border: 3px solid red; 
 
    } 
 
    input[type='submit'] { 
 
     background-color: #4CAF50; 
 
     border: none; 
 
     color: white; 
 
     padding: 16px 32px; 
 
     text-decoration: none; 
 
     margin: 4px 2px; 
 
     width: 100%; 
 
    } 
 
    input[value="Up"] { 
 
     background-color: #4CAF50; 
 
     border: none; 
 
     color: white; 
 
     padding: 16px 32px; 
 
     text-decoration: none; 
 
     margin: 4px 2px; 
 
    } 
 
    input[value="Down"] { 
 
     background-color: rgb(200, 0, 0); 
 
     border: none; 
 
     color: white; 
 
     padding: 16px 32px; 
 
     text-decoration: none; 
 
     margin: 4px 2px; 
 
    } 
 
    input[name="Add"] { 
 
     border: 3px solid #4CAF50; 
 
     color: white; 
 
     background-color: #253f3f; 
 
     padding: 16px 32px; 
 
     text-decoration: none; 
 
     font-weight: bold; 
 
     margin: 4px 2px; 
 
    } 
 
    </style> 
 
</head> 
 

 
<body> 
 

 
    <h1>Public Playlist</h1> 
 

 
    <table> 
 
    <tr> 
 
     <th> 
 
     <h2>Rank</h2> 
 
     </th> 
 
     <th> 
 
     <h2>Title</h2> 
 
     <h2>Author|Album</h2> 
 
     </th> 
 
     <th> 
 
     <h2>Score</h2> 
 
     </th> 
 
     <th> 
 
     <h2>Add</h2> 
 
     </th> 
 
    </tr> 
 
    <tr> 
 
     <form action="/add" method="GET"> 
 
     <td> 
 
      <h3 type="bold">1</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{name}}</h3> 
 
      <h3>{{artist}}:{{album}}</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{ score }}</h3> 
 
     </td> 
 
     <td> 
 
      <input type="button" name="Add" value="Add" id="Add"> 
 
     </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <form action="/add" method="GET"> 
 
     <td> 
 
      <h3 type="bold">2</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{name1}}</h3> 
 
      <h3>{{artist1}}:{{album1}}</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{ score1 }}</h3> 
 
     </td> 
 

 
     <td> 
 
      <input type="button" name="Add" value="Add1" id="Add1"> 
 
     </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <form action="/add" method="GET"> 
 

 
     <td> 
 
      <h3 type="bold">3</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{name2}}</h3> 
 
      <h3>{{artist2}}:{{album2}}</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{ score2 }}</h3> 
 
     </td> 
 

 
     <td> 
 
      <input type="button" name="Add" value="Add2" id="Add2"> 
 
     </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <form action="/add" method="GET"> 
 
     <td> 
 
      <h3 type="bold">4</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{name3}}</h3> 
 
      <h3>{{artist3}}:{{album3}}</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{ score3 }}</h3> 
 
     </td> 
 

 
     <td> 
 
      <input type="button" name="Add" value="Add3" id="Add3"> 
 
     </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <form action="/add" method="GET"> 
 
     <td> 
 
      <h3 type="bold">5</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{name4}}</h3> 
 
      <h3>{{artist4}}:{{album4}}</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{ score4 }}</h3> 
 
     </td> 
 

 
     <td> 
 
      <input type="button" name="Add" value="Add4" id="Add4"> 
 
     </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <form action="/add" method="GET"> 
 
     <td> 
 
      <h3 type="bold">6</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{name5}}</h3> 
 
      <h3>{{artist5}}:{{album5}}</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{ score5 }}</h3> 
 
     </td> 
 

 
     <td> 
 
      <input type="button" name="Add" value="Add5" id="Add5"> 
 
     </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <form action="/add" method="GET"> 
 
     <td> 
 
      <h3 type="bold">7</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{name6}}</h3> 
 
      <h3>{{artist6}}:{{album6}}</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{ score6 }}</h3> 
 
     </td> 
 
     <td> 
 
      <input type="button" name="Add" value="Add6" id="Add6"> 
 
     </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <form action="/add" method="GET"> 
 
     <td> 
 
      <h3 type="bold">8</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{name7}}</h3> 
 
      <h3>{{artist7}}:{{album7}}</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{ score7 }}</h3> 
 
     </td> 
 
     <td> 
 
      <input type="button" name="Add" value="Add7" id="Add7"> 
 
     </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <form action="/add" method="GET"> 
 
     <td> 
 
      <h3 type="bold">9</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{name8}}</h3> 
 
      <h3>{{artist8}}:{{album8}}</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{ score8 }}</h3> 
 
     </td> 
 

 
     <td> 
 
      <input type="button" name="Add" value="Add8" id="Add8"> 
 
     </td> 
 
     </form> 
 
    </tr> 
 
    <tr> 
 
     <form action="/add" method="GET"> 
 
     <td> 
 
      <h3 type="bold">10</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{name9}}</h3> 
 
      <h3>{{artist9}}:{{album9}}</h3> 
 
     </td> 
 
     <td> 
 
      <h3>{{ score9 }}</h3> 
 
     </td> 
 

 
     <td> 
 
      <input type="button" name="Add" value="Add9" id="Add9"> 
 
     </td> 
 
     </form> 
 
    </tr> 
 
    </table> 
 
</body> 
 

 
</html>

I hav dann e eine Python-Datei projectfalcon.py Die relevanten Methoden sind:

@app.route('/add', methods=['GET']) 
def addingSongs(): 
     if request.method == 'GET': 
      btnID = request.form['btn'] 
     if btnID == 'Add0': 
      songResults[0].upVote() 
      addedSongs.append(songResults[0]) 
      songResults.remove(0) 
     if btnID == 'Add1': 
      songResults[1].upVote() 
      addedSongs.append(songResults[1]) 
      songResults.remove(1) 
     return render_template('base.html') 

So die Base.html ist die erste Seite, die geladen wird. Sie geben einen Künstlernamen ein, um nach Liedern zu suchen, und Sie gelangen in die search_results.html, in der die Ergebnisse angezeigt werden. Ich versuche, den Benutzer in der Lage zu sein, auf die Schaltfläche Hinzufügen zu klicken, und der Song wird dann zu einer Liste in der Datei projectfalcon.py hinzugefügt. Nachdem du geklickt hast, gehst du zurück zur base.html Datei und lädst die Lieder neu, die ich in der Playlist nicht weiß, was ich falsch mache. Ich habe mehrere Male gegoogelt, aber vielleicht nicht die richtige Formulierung gefunden. Jede Hilfe wird geschätzt.

Antwort

0

Das offensichtlichste Problem, das ich erkennen kann, ist die Art und Weise, wie Sie auf die Variable btn zugreifen. Da Ihr Formular ein GET Anfrage sendet, muss es wie folgt zugegriffen werden:

btnID = request.args.get('btn', '') 

Abgesehen davon, dass ich nicht verstehe, wie Sie addedSongs Liste sind zu speichern. Der Flask-Kontext ist auf Anforderungen beschränkt, und Sie sollten keinen globalen Python-Namespace verwenden. Sie sollten entweder eine Kontextvariable (auch eine globale in flask) erstellen oder die einfachste Möglichkeit wäre, Benutzerdaten einfach in einer Datenbank zu speichern.

Sie sollten einen Blick auf this Flasch Tutorial, die eine vollständige Anleitung zum Erstellen einer Web-App von Grund auf neu ist. Vielleicht kann es von Nutzen sein, um die Grundlagen zu klären und Ihnen zu ermöglichen, die richtigen Formulierungen für Ihre Probleme zu finden! :)

Prost.

+0

Also so komme ich zu meinem Problem. Wenn ich die Seite lade, gehe ich zur base.html. Von dort aus klicke ich auf eine Schaltfläche und es wird in die Datei search_results.html verschoben und es werden einige Daten angezeigt. Ich möchte in der Lage sein, auf 1 von 10 Schaltflächen zu klicken und je nachdem, auf welchen Button ich klicke, macht es zwei Dinge. 1) es fügt das Lied der hinzugefügten Songliste hinzu 2) bringt den Benutzer zurück zur base.html. So hat momentan jeder Button den gleichen Namen "Add", aber jeder Wert ändert sich von "Add0" zu "Add1" usw. Ich möchte erreichen, welche Taste gedrückt wurde, dann den entsprechenden Song holen und an die hinzugefügte Liste anhängen. –

+0

Auch nicht sicher, warum ich nicht früher darüber nachgedacht habe, aber ich habe print ('testing') direkt unterhalb der addingSongs() -Funktion gesetzt und wenn ich auf einen Add-Button klicke, wird es nicht gedruckt. Daher denke ich, dass die Methode niemals aufgerufen wird. Ist etwas falsch mit meiner Approute möglich? –

+0

Es ist ziemlich schwierig, auf diese Weise zu debuggen. Aus den Dingen, die Sie tun möchten, empfehle ich zu verstehen, wie das Web funktioniert. Wie Ajax, Routing, Umleitung, Formulare funktionieren. Es wird viel einfacher sein, die Flasche so zu bekommen, wie Sie es wollen. Probiere auch ein Flaschentutorial aus, um dich mit den grundlegenden Konzepten wie 'url_for'-Umleitung, einfacherem Templating, Array-basierten Werten in Formularen, Ajax-Calls usw. vertraut zu machen. Ich empfehle [this] (https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-teil-ich-hallo-welt) tutorial. – oxalorg

Verwandte Themen