How to save file in database and a directory using flask SQLAlchemy?
up vote
1
down vote
favorite
I want to save age_s
, age_e
, gender
and filename
in SQLite database using codes below, but I have a problem.
When I upload the file, my file name is stored in the SQLite database, but the file itself is not stored in my system directory. How can I save a file in my system directory ?
I know that the problem is from the html file but I can not save the file in the directory and save the file name in the database, both actions at the same time.
app.py
@app.route("/new_contact", methods=('GET', 'POST'))
def new_contact():
'''
Create new contact
'''
form = ContactForm()
if form.validate_on_submit():
my_contact = Contact()
form.populate_obj(my_contact)
db.session.add(my_contact)
try:
db.session.commit()
# User info
flash('Contact created correctly', 'success')
return redirect(url_for('contacts'))
except:
db.session.rollback()
flash('Error generating contact.', 'danger')
return render_template('web/new_contact.html', form=form)
new_contact.html
{% extends 'layouts/master.html' %}
{% block title %}New contact{% endblock %}
{% block body %}
<h1>New contact</h1>
<form action="{{ url_for('new_contact') }}" method="post">
{{ generate_fields(form) }}
<input type="submit" class="btn btn-success" value="Add">
</form>
{% endblock %}
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField,SelectField,IntegerField,FileField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms.validators import DataRequired, Email, Length
class ContactForm(FlaskForm):
age_s = SelectField(u'age_s', choices=age_types)
age_e = SelectField(u'age_e', choices=age_types1)
#age_e = IntegerField('age_e', validators=[Length(min=-1, max=100, message='You cannot have more than 100 characters')])
gender = SelectField('gender', choices=gender_types)
#filename = StringField('filename', validators=[Length(min=-1, max=20, message='You cannot have more than 20 characters')])
filename = FileField()
python flask flask-sqlalchemy
add a comment |
up vote
1
down vote
favorite
I want to save age_s
, age_e
, gender
and filename
in SQLite database using codes below, but I have a problem.
When I upload the file, my file name is stored in the SQLite database, but the file itself is not stored in my system directory. How can I save a file in my system directory ?
I know that the problem is from the html file but I can not save the file in the directory and save the file name in the database, both actions at the same time.
app.py
@app.route("/new_contact", methods=('GET', 'POST'))
def new_contact():
'''
Create new contact
'''
form = ContactForm()
if form.validate_on_submit():
my_contact = Contact()
form.populate_obj(my_contact)
db.session.add(my_contact)
try:
db.session.commit()
# User info
flash('Contact created correctly', 'success')
return redirect(url_for('contacts'))
except:
db.session.rollback()
flash('Error generating contact.', 'danger')
return render_template('web/new_contact.html', form=form)
new_contact.html
{% extends 'layouts/master.html' %}
{% block title %}New contact{% endblock %}
{% block body %}
<h1>New contact</h1>
<form action="{{ url_for('new_contact') }}" method="post">
{{ generate_fields(form) }}
<input type="submit" class="btn btn-success" value="Add">
</form>
{% endblock %}
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField,SelectField,IntegerField,FileField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms.validators import DataRequired, Email, Length
class ContactForm(FlaskForm):
age_s = SelectField(u'age_s', choices=age_types)
age_e = SelectField(u'age_e', choices=age_types1)
#age_e = IntegerField('age_e', validators=[Length(min=-1, max=100, message='You cannot have more than 100 characters')])
gender = SelectField('gender', choices=gender_types)
#filename = StringField('filename', validators=[Length(min=-1, max=20, message='You cannot have more than 20 characters')])
filename = FileField()
python flask flask-sqlalchemy
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to save age_s
, age_e
, gender
and filename
in SQLite database using codes below, but I have a problem.
When I upload the file, my file name is stored in the SQLite database, but the file itself is not stored in my system directory. How can I save a file in my system directory ?
I know that the problem is from the html file but I can not save the file in the directory and save the file name in the database, both actions at the same time.
app.py
@app.route("/new_contact", methods=('GET', 'POST'))
def new_contact():
'''
Create new contact
'''
form = ContactForm()
if form.validate_on_submit():
my_contact = Contact()
form.populate_obj(my_contact)
db.session.add(my_contact)
try:
db.session.commit()
# User info
flash('Contact created correctly', 'success')
return redirect(url_for('contacts'))
except:
db.session.rollback()
flash('Error generating contact.', 'danger')
return render_template('web/new_contact.html', form=form)
new_contact.html
{% extends 'layouts/master.html' %}
{% block title %}New contact{% endblock %}
{% block body %}
<h1>New contact</h1>
<form action="{{ url_for('new_contact') }}" method="post">
{{ generate_fields(form) }}
<input type="submit" class="btn btn-success" value="Add">
</form>
{% endblock %}
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField,SelectField,IntegerField,FileField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms.validators import DataRequired, Email, Length
class ContactForm(FlaskForm):
age_s = SelectField(u'age_s', choices=age_types)
age_e = SelectField(u'age_e', choices=age_types1)
#age_e = IntegerField('age_e', validators=[Length(min=-1, max=100, message='You cannot have more than 100 characters')])
gender = SelectField('gender', choices=gender_types)
#filename = StringField('filename', validators=[Length(min=-1, max=20, message='You cannot have more than 20 characters')])
filename = FileField()
python flask flask-sqlalchemy
I want to save age_s
, age_e
, gender
and filename
in SQLite database using codes below, but I have a problem.
When I upload the file, my file name is stored in the SQLite database, but the file itself is not stored in my system directory. How can I save a file in my system directory ?
I know that the problem is from the html file but I can not save the file in the directory and save the file name in the database, both actions at the same time.
app.py
@app.route("/new_contact", methods=('GET', 'POST'))
def new_contact():
'''
Create new contact
'''
form = ContactForm()
if form.validate_on_submit():
my_contact = Contact()
form.populate_obj(my_contact)
db.session.add(my_contact)
try:
db.session.commit()
# User info
flash('Contact created correctly', 'success')
return redirect(url_for('contacts'))
except:
db.session.rollback()
flash('Error generating contact.', 'danger')
return render_template('web/new_contact.html', form=form)
new_contact.html
{% extends 'layouts/master.html' %}
{% block title %}New contact{% endblock %}
{% block body %}
<h1>New contact</h1>
<form action="{{ url_for('new_contact') }}" method="post">
{{ generate_fields(form) }}
<input type="submit" class="btn btn-success" value="Add">
</form>
{% endblock %}
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField,SelectField,IntegerField,FileField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms.validators import DataRequired, Email, Length
class ContactForm(FlaskForm):
age_s = SelectField(u'age_s', choices=age_types)
age_e = SelectField(u'age_e', choices=age_types1)
#age_e = IntegerField('age_e', validators=[Length(min=-1, max=100, message='You cannot have more than 100 characters')])
gender = SelectField('gender', choices=gender_types)
#filename = StringField('filename', validators=[Length(min=-1, max=20, message='You cannot have more than 20 characters')])
filename = FileField()
python flask flask-sqlalchemy
python flask flask-sqlalchemy
edited Nov 20 at 10:13
Dinko Pehar
8192324
8192324
asked Nov 20 at 6:31
dafi mock
245
245
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387469%2fhow-to-save-file-in-database-and-a-directory-using-flask-sqlalchemy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown