Ahmad Ilham
Ahmad Ilham

Reputation: 1

How to query with many tables

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy 
#import sqlite3 as sql 

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://ahmad:[email protected]/utama'
db = SQLAlchemy(app)


class ak(db.Model):
    __tablename__ = 'ak'
    id = db.Column(db.Integer, primary_key=True)
    nama = db.Column(db.String)
    alamat = db.Column(db.String)
    akreditasi = db.Column(db.String)
    def __init__(self, id, nama, alamat, akreditasi):
        self.id = id
        self.city = nama
        self.alamat = alamat
        self.akreditasi = akreditasi

class av(db.Model):
    __tablename__ = 'av'
    id = db.Column(db.Integer, primary_key=True)
    nama = db.Column(db.String)
    alamat = db.Column(db.String)
    akreditasi = db.Column(db.String)
    def __init__(self, id, nama, alamat, akreditasi):
        self.id = id
        self.city = nama
        self.alamat = alamat
        self.akreditasi = akreditasi

id_jurusan = db.Table('id_jurusan',
    db.Column('id', db.Integer, db.ForeignKey('ak.id')),
    db.Column('id', db.Integer, db.ForeignKey('av.id'))
    )
@app.route('/ak')
def jurusan(jurusan):
    return render_template('index.html', rows=ak.query.all() )

@app.route('/av')
def Akuntansi():
    return render_template('index.html', rows=av.query.all() )

if __name__ == '__main__':
    app.run(debug=True, host='1.1.1.1', port=80)

I am a new to learn python, in this case I studied the framework flask and I had trouble on the declaration SQLAlchemy, precisely displays the contents of the table but with the same structure,when executed will be like this.....

[ enter image description here

which one success

enter image description here

Upvotes: 0

Views: 68

Answers (1)

rob2universe
rob2universe

Reputation: 7628

You are using the decorator

@app.route('/av')

The method which succeeds Akuntansi() does not require a parameter. So this works. The method which fails expects a parameter jurusan(jurusan) but your decorator @app.route('/ak') does not consider this.

To pass a parameter you need to use the decorator like this: @app.route("/ak/<jurusan>") and then also pass the parameter in the request.

Upvotes: 1

Related Questions