2016-10-31 3 views
0

私はPythonフラスコを初めて使用しているため、404見つからないエラーを取り除きたいと考えています。私は、sqlliteテーブルに挿入し、挿入された項目をリストします。助けてください。おかげ404が見つかりません。 sqlliteを使用してsqlliteを使用してデータを入力する

views.py

from flask import Flask, render_template, request, url_for, redirect 
import sqlite3 as sql 

app = Flask(__name__) 

@app.route('/') 
def home(): 
    return render_template('home.html') 

# route for handling the login page logic 
@app.route('/login', methods=['GET', 'POST']) 
def login(): 
    error = None 
    if request.method == 'POST': 
     if request.form['username'] != 'admin' or request.form['password'] != 'admin': 
      error = 'Invalid Credentials. Please try again.' 
     else: 
      return redirect(url_for('index')) 
    return render_template('login.html', error=error) 

@app.route('/enternew') 
def new_student(): 
    return render_template('student.html') 

@app.route('/addrec', methods=['POST', 'GET']) 
def addrec(): 
    if request.method == 'POST': 
     try: 
      nm = request.form['nm'] 
      addr = request.form['add'] 
      city = request.form['city'] 
      pin = request.form['pin'] 

      with sql.connect("databsae.db") as con: 
       cur = con.cursor() 

       cur.execute("INSERT INTO students (name,addr,city,pin) VALUES(?, ?, ?, ?)",(nm,addr,city,pin)) 

       con.commit() 
       msg = "Record successfully added" 
     except: 
      con.rollback() 
      msg = "error in insert operation" 

     finally: 
      return render_template("result.html", msg=msg) 
      con.close() 

@app.route('/list') 
def list(): 
    con = sql.connect("databsae.db") 
    con.row_factory = sql.Row 

    cur = con.cursor() 
    cur.execute("select * from students") 

    rows = cur.fetchall(); 
    return render_template("list.html", rows=rows) 

if __name__ == '__main__': 
    app.run() 

students.html

<html> 
    <body> 

     <form action = "{{ url_for('addrec') }}" method = "POST"> 
     <h3>Student Information</h3> 
     Name<br> 
     <input type = "text" name = "nm" /></br> 

     Address<br> 
     <textarea name = "add" ></textarea><br> 

     City<br> 
     <input type = "text" name = "city" /><br> 

     PINCODE<br> 
     <input type = "text" name = "pin" /><br> 
     <input type = "submit" value = "submit" /><br> 
     </form> 

    </body> 
</html> 

するlist.html

<table border = 1> 
    <thead> 
     <td>Name</td> 
     <td>Address>/td< 
     <td>city</td> 
     <td>Pincode</td> 
    </thead> 
    {% for row in rows %} 
     <tr> 
      <td>{{row["name"]}}</td> 
      <td>{{row["addr"]}}</td> 
      <td> {{ row["city"]}}</td> 
      <td>{{row['pin']}}</td> 
     </tr> 
    {% endfor %} 
    </table> 

    <a href = "/">Go back to home page</a> 

result.html

<!doctype html> 
<html> 
    <body> 

     result of addition : {{ msg }} 
     <h2><a href = "/">go back to home page</a></h2> 

    </body> 
</html> 
+0

なぜlist.htmlにtdの奇妙なフォーマットがありますか? ​​>/td < – toha

+0

404エラーが見つかりません。 –

+0

いつあなたは404を手に入れますか?テンプレートをレンダリングするときに起こりますか?特定のエンドポイントにアクセスするときに発生しますか? – dirn

答えて

0

変更は、この

# route for handling the login page logic 
@app.route('/login', methods=['GET', 'POST']) 
def login(): 
    error = None 
    if request.method == 'POST': 
     if request.form['username'] != 'admin' or request.form['password'] != 'admin': 
      error = 'Invalid Credentials. Please try again.' 
     else: 
      return redirect(url_for('home')) 
    return render_template('login.html', error=error) 

へのログイン方法は、私が変更さ唯一の事はreturn redirect(url_for('home'))ました。 indexメソッドは存在しませんでしたが、url_for('index')にリダイレクトされました。

関連する問題