var id = '#id_name_01'
var pattern = /\d+/
var id_only_digits = id.match(pattern);
--
>>> id_only_digits
<<< ["01"]
$('#id_name_01').click(function(){
var id_digits = $(this).attr('id').match(pattern)[0];
$('#other_id_' + id_digits).somefunction();
});
from sqlalchemy import Column, Integer, String
>>> class User(Base):
... __tablename__ = 'users'
...
... id = Column(Integer, primary_key=True)
... name = Column(String)
... fullname = Column(String)
... password = Column(String)
...
... def __repr__(self):
... return "<User(name='%s', fullname='%s', password='%s')>" % (
... self.name, self.fullname, self.password)
from sqlalchemy import inspect
...
def __repr__(self):
mapper = inspect(self).mapper
ent = []
for col in mapper.column_attrs:
ent.append("{0}={1}".format(col.key, getattr(self, col.key)))
return "<{0}(".format(self.__class__.__name__) + ", ".join(ent) + ")>"
session.query(User).join(UserItem, Lang)
session.query(UserItem.id, UserItem.date, UserItem.item_name, Lang.desc_rus, User.email).join(User, Lang)
{{ row.ray.username }}
rows = Weather.query.outerjoin(User, Weather.ray_id==User.id).order_by(User.name.asc())
rows = Weather.query.outerjoin(User).order_by(User.name.asc())
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tst.db'
db = SQLAlchemy(app)
import datetime
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
username = db.Column(db.String(120), unique=True)
password = db.Column(db.String(120))
def __init__(self, name, username, password):
self.name = name
self.username = username
self.password = password
class Weather(db.Model):
__tablename__ = 'weather'
id = db.Column(db.Integer, primary_key=True)
ray_id = db.Column(db.Integer, db.ForeignKey('users.id'))
ray = db.relationship('User', backref=db.backref('weather_current', uselist=False))
putdate = db.Column(db.DateTime)
sost = db.Column(db.String(250))
temp = db.Column(db.String(150))
def __init__(self, ray_id, putdate, sost, temp):
self.ray_id = ray_id
self.putdate = putdate
self.sost = sost
self.temp = temp
db.drop_all()
db.create_all()
u = User("usertest2", 'testuser2', '123')
db.session.add(u)
db.session.commit()
db.session.add(Weather(u.id, datetime.datetime.now(), 'test', '123'))
db.session.add(Weather(None, datetime.datetime.now(), 'test', '123'))
db.session.commit()
@app.route('/', methods = ['GET', 'POST'])
def weather():
rows = Weather.query.outerjoin(User).order_by(User.name.asc())
return render_template("weather.html",
rows = rows)
if __name__ == "__main__":
app.run(debug=True)
<table>
{% for row in rows %}
<tr>
<td align=left>{{ row.ray.name }}</td>
<td>{{ row.putdate }}</td>
<td>{{ row.sost }}</td>
<td>{{ row.temp }}</td>
<td>{{ row.ray.username }}</td>
</tr>
{% endfor %}
</table>
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy as SA
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://user:pass@localhost:3306/app_db'
db = SA(app)
from sqlalchemy import create_engine
db.metadata.reflect(db.engine)
from Classes import *
@app.route('/')
def get_index():
Users.query.all()
return 'Hello World!'
if __name__ == '__main__':
app.run(debug=True)
pass
Я рекомендую Flask. У Django, к сожалению, слишком сильная привязка к своему ORM, да и не только. Для flask есть очень много разных расширений, та же админка. По ORM, можно использовать как SQLAlchemy, так и вообще что по душе будет. А еще, есть такая вкусняшка для jinja2 — pyjade, позволяет писать шаблоны в стиле Jade/Haml. Очень клевая вещь =)