Herramientas de usuario

Herramientas del sitio


comb:webapp0

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
comb:webapp0 [2014/07/13 14:12]
alfred [Install JS libraries]
comb:webapp0 [2020/05/09 09:25] (actual)
Línea 46: Línea 46:
  
 ===== Model ===== ===== Model =====
 +==== Interact with model classes ====
 +Only importing the code file and using the class you will be interacting with the data base:
 +<code python>
 +from person_app import Person
 +from person_app import db
 +p = Person(name='​Juan',​ age=33)
 +Person.query.all()
 +db.session.add(p)
 +db.session.commit()
 +</​code>​
 ==== Relationships with SQLAlchemy ==== ==== Relationships with SQLAlchemy ====
 === One to one === === One to one ===
Línea 83: Línea 93:
     id = db.Column(db.Integer,​ primary_key=True)     id = db.Column(db.Integer,​ primary_key=True)
 </​code>​ </​code>​
 +=== Define fields ===
 +<code python>
 +class Deck(db.Model):​
 +    id = db.Column(db.Integer,​ primary_key=True)
 +    name = db.Column(db.Text,​ nullable=False)
 +    date = db.Column(db.DateTime,​ default=datetime.now())
 +    cards = db.relationship('​Card',​ backref='​deck',​ lazy='​dynamic'​)
  
 +
 +class Card(db.Model):​
 +    id = db.Column(db.Integer,​ primary_key=True)
 +    id_deck = db.Column(db.Integer,​
 +                        db.ForeignKey('​deck.id'​),​
 +                        nullable=False)
 +    front = db.Column(db.Text)
 +    rever = db.Column(db.Text)
 +</​code>​
 +=== Separate model in another file ===
 +The ''​flaskext.sqlalchemy''​ module does not have to be initialized with the app right away - you can do this instead:
 +<code python>
 +# apps.members.models
 +from flaskext.sqlalchemy import SQLAlchemy
 +
 +db = SQLAlchemy()
 +
 +class Member(db.Model):​
 +    # fields here
 +    pass
 +</​code>​
 +And then in your application setup you can call init_app:
 +<code python>
 +# apps.application.py
 +from flask import Flask
 +from apps.members.models import db
 +
 +app = Flask(__name__)
 +# later on
 +db.init_app(app)
 +</​code>​
 +
 +Example:
 +<code python>
 +# apps.shared.models
 +from flaskext.sqlalchemy import SQLAlchemy
 +
 +db = SQLAlchemy()
 +</​code>​
 +<code python>
 +# apps.members.models
 +from apps.shared.models import db
 +
 +class Member(db.Model):​
 +    # TODO: Implement this.
 +    pass
 +</​code>​
 +<code python>
 +# apps.reporting.members
 +from flask import render_template
 +from apps.members.models import Member
 +
 +def report_on_members():​
 +    # TODO: Actually use arguments
 +    members = Member.filter(1==1).all()
 +    return render_template("​report.html",​ members=members)
 +</​code>​
 +<code python>
 +# apps.reporting.routes
 +from flask import Blueprint
 +from apps.reporting.members import report_on_members
 +
 +reporting = Blueprint("​reporting",​ __name__)
 +
 +reporting.route("/​member-report",​ methods=["​GET","​POST"​])(report_on_members)
 +</​code>​
 +<code python>
 +# apps.application
 +from flask import Flask
 +from apps.shared import db
 +from apps.reporting.routes import reporting
 +
 +app = Flask(__name__)
 +db.init_app(app)
 +app.register_blueprint(reporting)
 +</​code>​
 +
 +Another example:
 +<code python>
 +app = Flask(__name__,​ static_path=''​)
 +app.config['​SQLALCHEMY_DATABASE_URI'​] = '​sqlite:///​digin.db'​
 +db.init_app(app)
 +with app.app_context():​
 +    db.create_all()
 +</​code>​
 ===== HTML and JavaScript ===== ===== HTML and JavaScript =====
 ==== Install JS libraries ==== ==== Install JS libraries ====
Línea 91: Línea 193:
 $ sudo npm install -g bower $ sudo npm install -g bower
 $ sudo ln -s /​usr/​bin/​nodejs /​usr/​bin/​node $ sudo ln -s /​usr/​bin/​nodejs /​usr/​bin/​node
 +Inside the static folder...
 +$ bower install angular
 </​code>​ </​code>​
comb/webapp0.1405260726.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)