在我能找到的所有博客和/或视频上都有。我刚接触JS/AJAX/JQUERY,最近尝试将这些家伙合并到我的Flask应用程序中,这让我现在很沮丧。尝试通过AJAX调用将表单数据提交到MySQL数据库,而不刷新页面。就我所知。甚至都看不到督察提出的职位要求。也许它没按路线走?这是我的密码:
表单HTML
<form name="race_form" id="race_form">
<input type="text" id="characterID" value="{{ character.id }}" />
<input type="text" id="raceID" value="{{ attribute.id }}" />
<button type="submit" id="toggleoption-{{ attribute.attribute_name }}"
class="btn btn-default char_gen_options_btn" style="margin: 10px 0px 0px 10px;
background:url(/static/img/blacksmith.jpg) right center no-repeat; background-size:contain;
background-color:#B5B6AF;font-weight:900;">Select {{ attribute.attribute_name }}</button>
</form>
阿贾克斯
$(document).ready(function() {
$('#race_form').submit(function(event) {
event.preventDefault();
var character_id = $('#characterID').val();
var attribute_id = $('#raceID').val();
$.ajax({
url: "{{ url_for('auth.register_race') }}",
type: 'POST',
data: { character_id: character_id, attribute_id: attribute_id }
});
});
});
烧瓶视图(提交到多对多表)
@auth.route('/register_race', methods=['POST'])
@login_required
def register_race():
character = Character.query.filter_by(id=request.form['characterID']).first()
character.character_attributes.character_id = request.form['characterID']
character.character_attributes.attribute_id = request.form['raceID']
db.session.commit()
return jsonify({'result': 'success'})
还有桌子型号.py视图所引用的:
class Character(db.Model):
"""
Create a Player Character table - all characters assigned to user
"""
__tablename__ = 'characters'
id = db.Column(db.Integer, primary_key=True, unique=True)
character_name = db.Column(db.String(60), index=True, unique=True)
experience_points = db.Column(db.Integer) # Refers to proprietary character build points
character_lives = db.Column(db.Integer, default=0)
character_img = db.Column(db.String(300)) # Path to character img on server
create_date = db.Column(db.DateTime, default=func.now())
last_update = db.Column(db.DateTime, onupdate=func.now())
# Keys
users = db.relationship('User', secondary='user_character')
attribute = db.relationship('Attribute', secondary='character_attributes')
items = db.relationship('Items', secondary='inventory')
files = db.relationship('File', secondary="character_files")
def __repr__(self):
return '<Character Name: {}>'.format(self.character_name)
class CharacterAttributes(db.Model):
"""
Many to many table for characters and assigned attributes
"""
__tablename__ = 'character_attributes'
id = db.Column(db.Integer, primary_key=True, unique=True)
create_date = db.Column(db.DateTime, default=func.now())
last_update = db.Column(db.DateTime, onupdate=func.now())
amount_purchased = db.Column(db.Integer)
# Keys
character_id = db.Column(db.Integer, db.ForeignKey('characters.id'))
attribute_id = db.Column(db.Integer, db.ForeignKey('attributes.id'))
character = db.relationship('Character', backref=backref('character_attributes', cascade='all, delete-orphan'))
attribute = db.relationship('Attribute', backref=backref('character_attributes', cascade='all, delete-orphan'))
目前没有回答
相关问题 更多 >
编程相关推荐