Created
July 7, 2020 11:23
-
-
Save emregeldegul/db4338d452e117af8222a418ba82156e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import render_template, Flask, jsonify | |
from flask_wtf import FlaskForm | |
from wtforms import StringField, SubmitField | |
app = Flask(__name__) | |
app.secret_key = 'CokGizliKey' | |
class UserCreateForm(FlaskForm): | |
name = StringField('Name') | |
surname = StringField('Surname') | |
submit = SubmitField('Submit') | |
@app.route('/create', methods = ['GET']) | |
def create(): | |
form = UserCreateForm() | |
return render_template('create.html', form=form) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/store', methods = ['POST']) | |
def store(): | |
form = UserCreateForm() | |
if form.validate_on_submit(): | |
data = {'name': form.name.data, 'surname': form.surname.data} | |
return jsonify(data = data) | |
return jsonify(data = form.errors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<html> | |
<head> | |
<title>Ajax ile WTForms Post İşlemi</title> | |
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script> | |
</head> | |
<body> | |
<form id="form" action="" method="POST"> | |
{{ form.hidden_tag() }} | |
{{ form.name.label() }}<br> | |
{{ form.name() }} <br><br> | |
{{ form.surname.label() }}<br> | |
{{ form.surname() }}<br><br> | |
{{ form.submit }} | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
$(document).ready(function() { | |
$('form').submit(function (e) { | |
var url = "{{ url_for('store') }}"; | |
$.ajax({ | |
type: "POST", | |
url: url, | |
data: $('form').serialize(), | |
success: function (data) { | |
console.log(data) | |
} | |
}); | |
e.preventDefault(); // Formun 'geleneksel' olarak gönderilmesini engeller. | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment