Tôi là khá mới vào khung bình và đã tạo một trang hồ sơ chỉnh sửa cho một webportal. Tôi bị kẹt tại một điểm và không thể tự động điền một biểu mẫu.Pre-Populate một WTforms trong bình, với dữ liệu từ một đối tượng SQLAlchemy
Đây là lớp mẫu của tôi:
class EditProfile(Form):
username = TextField('Username', [Required()])
email = TextField('Email', [Required()])
about = TextAreaField('About', [Required()])
website = TextField('Website', [Required()])
Đây là chức năng của tôi mà đánh giá biểu mẫu.
def editprofile(nickname = None):
if g.fas_user['username'] == nickname or request.method == 'POST':
form = EditProfile()
form_action = url_for('profile.editprofile')
if request.method == 'POST' and form.validate():
if form.username.data == nickname :
query = EditProfile(form.username.data,
form.email.data,
form.about.data,
form.website.data,
)
print query #debug
db.session.add(query)
db.session.commit()
flash('User Updated')
print "added"
return(url_for('profile.editprofile'))
return render_template('profile/add.html', form=form,
form_action=form_action, title="Update Profile")
else:
return "Unauthorised"
Và template html của tôi cho hình thức là hình thức là:
{% extends "base.html" %}
{% block title %}
{{ title }}
{% endblock %}
{% block content %}
{% from "_formhelpers.html" import render_field %}
<div id="Edit Profile">
<h2>{{ title }}</h2>
<form method="post" action="{{ form_action }}">
<fieldset>
<legend></legend>
{{ render_field(form.username) }}
{{ render_field(form.email)}}
{{ render_field(form.about)}}
{{ render_field(form.website) }}
</fieldset>
<input type="submit" class="button" value="Save"/>
</form>
</div>
{% endblock %}
tôi có một đối tượng, về thành viên lớp. Và từ đối tượng đó tôi muốn điền vào biểu mẫu này. Làm cách nào tôi có thể chuẩn bị trước các giá trị trong biểu mẫu. Tôi đang cố triển khai chức năng chỉnh sửa hồ sơ ở đây.
tôi vẫn một chút bối rối. Một khi tôi đang gửi đối tượng người dùng của tôi trong các hình thức tôi cần phải chỉnh sửa các mẫu tôi cũng nghĩ, nếu không thì làm thế nào nó sẽ kết hợp đúng feild chính xác với các mô hình? Vì các thành viên dữ liệu trong mô hình người dùng và lớp biểu mẫu của tôi có các tên khác nhau. –
Tại sao chúng khác nhau? Ngoài ra, bạn có thể cho chúng tôi thấy mô hình không? – dirn
https://github.com/hammadhaleem/fedora-college/blob/develop/fedora_college/core/models.py Đây là các mô hình của tôi. –