Jestem nowością do Pythona Kolbę, moja aktualna aplikacja ma formularz, który pomaga użytkownikom aktualizować swój profil i będzie wyświetlany na backend Użytkownicy, ale po napisaniu każdej linii kodów kody nie są wyświetlane błędem po przesłaniu, ale nie dostaję wszystkich Poświadczenia wprowadzone na formularzu.

Oto trasa ustawień profilu

@app.route("/profile/settings", methods=["GET", "POST"])
@login_required
def profile_settings():
    #education = Education.query.filter_by(user=current_user).first()
    form = UpdateProfileForm()
    if form.validate_on_submit():
        current_user.aboutme = form.aboutme.data
        current_user.phone = form.phone.data
        current_user.location = form.location.data
        current_user.facebook = form.facebook.data
        current_user.twitter = form.twitter.data
        current_user.linkedin = form.linkedin.data
        current_user.school = form.school.data
        current_user.course = form.course.data
        current_user.fromDate = form.fromDate.data
        current_user.toDate = form.toDate.data
        current_user.description = form.description.data
        current_user.institution = form.institution.data
        db.session.commit()
        flash("Your profile have been updated", "danger")
        return redirect(url_for('profile_settings'))

    elif request.method == 'GET':
        form.aboutme.data = current_user.aboutme
        form.phone.data = current_user.phone
        form.location.data = current_user.location
        form.facebook.data = current_user.facebook
        form.twitter.data = current_user.twitter
        form.linkedin.data = current_user.linkedin
        form.school.data = current_user.school
        form.course.data = current_user.course
        form.fromDate.data = current_user.fromDate
        form.toDate.data = current_user.toDate
        form.institution.data = current_user.institution
        form.description.data = current_user.description
    return render_template("profile-settings.html",
                           title="Profile Setting",
                           form=form)

I oto formularz

class UpdateProfileForm(FlaskForm):
    picture = FileField('Change Profile Picture',
                        validators=[FileAllowed(['jpg', 'png'])])
    aboutme = TextAreaField("About Me", validators=[DataRequired()])
    phone = TextField("Phone Number", validators=[DataRequired()])
    location = StringField("Location", validators=[DataRequired()])
    facebook = StringField("Facebook", validators=[DataRequired()])
    twitter = StringField("Twitter", validators=[DataRequired()])
    linkedin = StringField("Linkedin", validators=[DataRequired()])
    school = StringField("School", validators=[DataRequired()])
    institution = StringField("Institution", validators=[DataRequired()])
    course = StringField("Course", validators=[DataRequired()])
    fromDate = DateField("From", validators=[DataRequired()])
    toDate = DateField("To", validators=[DataRequired()])
    description = TextAreaField("Description", validators=[DataRequired()])
    submit = SubmitField("Update")

Kod HTML jest tutaj ...

{% extends 'layout.html' %}

{% block content %}

<!-- To make contents vertically centered -->
<div style="height: 100vh;" class="container">
    <div class="row h-100">
        <div class="col-sm-12 my-auto">
            <div class="mx-auto w-75 card p-4 shadow-lg w-sm">

                <!-- Header Logo-->
                <div class="text-center animate__animated animate__fadeIn">
                    <h1 class="font-weight-500 logo">Profile <span class="text-danger">Setting</span></h1>

                    <hr class="mt-4">
                </div>

                <!-- Form -->
                <form action="" method="POST">
                    {{ form.hidden_tag() }}

                    <!-- Firstname(fname) and lastname(lname) Input -->
                    <div class="card p-3">
                        <h5 class="border-bottom">
                            About Me
                        </h5>
                        <div class="form-group">
                            <label class="small mb-2" for="inputAboutMe">Write a paragraph about yourself</label>
                            {% if form.aboutme.errors %}
                            {{ form.aboutme(placeholder="Write a paragraph about yourself", class="form-control is-invalid", rows=7) }}
                            <div class="invalid-feedback">
                                {% for error in form.aboutme.errors %}
                                <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                            {% else %}
                            {{ form.aboutme(placeholder="Write a paragraph about yourself", class="form-control", rows=7) }}
                            {% endif %}
                        </div>
                    </div>

                    <hr>

                    <!-- Users Phone and Location -->
                    <div class="card p-3">
                        <h5 class="border-bottom">
                            Contact Information
                        </h5>
                        <div class="form-group">
                            <label class="small mb-2" for="inputPhoneNumber">Phone Number</label>
                            {% if form.phone.errors %}
                            {{ form.phone(placeholder="08012345678", class="form-control is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.phone.errors %}
                                <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                            {% else %}
                            {{ form.phone(placeholder="08012345678", class="form-control") }}
                            {% endif %}
                        </div>

                        <div class="form-group">
                            <label class="small mb-2" for="inputLocation">Location</label>
                            {% if form.location.errors %}
                            {{ form.location(placeholder="Your Location", class="form-control is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.location.errors %}
                                <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                            {% else %}
                            {{ form.location(placeholder="Your Location", class="form-control") }}
                            {% endif %}
                        </div>
                    </div>

                    <hr>

                    <!-- Social Media Accounts (Facebook, Twitter, LinkedIn) -->
                    <div class="card p-3">
                        <h5 class="border-bottom">
                            Socail Media Info
                        </h5>
                        <div class="form-group">
                            <label class="small mb-2" for="inputFacebook">Facebook</label>
                            {% if form.facebook.errors %}
                            {{ form.facebook(placeholder="Facebook", class="form-control is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.facebook.errors %}
                                <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                            {% else %}
                            {{ form.facebook(placeholder="Facebook", class="form-control") }}
                            {% endif %}
                        </div>

                        <div class="form-group">
                            <label class="small mb-2" for="inputTwitter">Twitter</label>
                            {% if form.twitter.errors %}
                            {{ form.twitter(placeholder="Twitter", class="form-control is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.twitter.errors %}
                                <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                            {% else %}
                            {{ form.twitter(placeholder="Twitter", class="form-control") }}
                            {% endif %}
                        </div>

                        <div class="form-group">
                            <label class="small mb-2" for="inputLinkedin">Linkedin</label>
                            {% if form.linkedin.errors %}
                            {{ form.linkedin(placeholder="Linkedin", class="form-control is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.linkedin.errors %}
                                <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                            {% else %}
                            {{ form.linkedin(placeholder="Linkedin", class="form-control") }}
                            {% endif %}
                        </div>
                    </div>

                    <hr>

                    <!-- Education (School, Institution, Date, Description) Input -->
                    <div class="card p-3">
                        <h5 class="border-bottom">
                            Education
                        </h5>

                        <div class="form-group">
                            <label class="small mb-2" for="inputSchool">School</label>
                            {% if form.school.errors %}
                            {{ form.school(placeholder="School", class="form-control is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.school.errors %}
                                <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                            {% else %}
                            {{ form.school(placeholder="School", class="form-control") }}
                            {% endif %}
                        </div>

                        <div class="form-group">
                            <label class="small mb-2" for="inputSchool">Institution</label>
                            {% if form.institution.errors %}
                            {{ form.institution(placeholder="Univeristy or College", class="form-control is-invalid") }}
                            <div class="invalid-feedback">
                                {% for error in form.institution.errors %}
                                <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                            {% else %}
                            {{ form.institution(placeholder="Univeristy or College", class="form-control") }}
                            {% endif %}
                        </div>

                        <div class="form-group">
                            <label class="small mb-2" for="inputCourse">Course</label>
                            {% if form.course.errors %}
                            {{ form.course(placeholder="Course", class="form-control is-invalid")}}
                            <div class="invalid-feedback">
                                {% for error in form.course.errors %}
                                <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                            {% else %}
                            {{ form.course(placeholder="Course", class="form-control")}}
                            {% endif %}
                        </div>


                        <div class="form-group">
                            <div class="input-group">
                                {% if form.fromDate.errors %}
                                {{ form.fromDate(placeholder="From", class="form-control is-invalid") }}
                                <div class="invalid-feedback">
                                    {% for error in form.fromDate.errors %}
                                    <span>{{ error }}</span>
                                    {% endfor %}
                                </div>
                                {% else %}
                                {{ form.fromDate(placeholder="From", class="form-control") }}
                                {% endif %}

                                {% if form.toDate.errors %}
                                {{ form.toDate(placeholder="To", class="form-control is-invalid") }}
                                <div class="invalid-feedback">
                                    {% for error in form.toDate.errors %}
                                    <span>{{ error }}</span>
                                    {% endfor %}
                                </div>
                                {% else %}
                                {{ form.toDate(placeholder="To", class="form-control ml-3") }}
                                {% endif %}
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="small mb-2" for="inputDescription">Description</label>
                            {% if form.description.errors %}
                            {{ form.description(placeholder="Write about your education", class="form-control is-invalid", rows=6) }}
                            <div class="invalid-feedback">
                                {% for error in form.description.errors %}
                                <span>{{ error }}</span>
                                {% endfor %}
                            </div>
                            {% else %}
                            {{ form.description(placeholder="Write about your education", class="form-control", rows=6) }}
                            {% endif %}
                        </div>
                    </div>

                    <hr>

                    <!-- Profile Picture -->
                    <div class="card p-3">
                        <h5 class="border-bottom">
                            Change Profile Picture
                        </h5>
                        <div class="form-group">
                            {{ form.picture(class="form-control-file") }}
                            {% if form.picture.errors %}
                            {% for error in form.picture.errors %}
                            <span class="text-danger">{{ error }}</span></br>
                            {% endfor %}
                            {% endif %}
                        </div>
                    </div>


                    <!-- Update Button -->
                    <button type="submit" class="form-control btn btn-primary mt-4">Update</button>

                    <hr>

                </form>

                <!-- Flash Messages -->
                {% with messages = get_flashed_messages(with_categories=true) %}
                {% if messages%}
                {% for category, message in messages %}
                <div class="mt-3 alert alert-danger rounded text-center text-danger p-3 font-weight-bold"><span
                        class="{{ category }}"></span>{{ message }}</div>
                {% endfor %}
                {% endif %}
                {% endwith %}
            </div>

        </div>
    </div>
</div>
{% endblock content %}

A oto model użytkownika w bazie danych, używam Sqlalchemy

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    fname = db.Column(db.String(60), nullable=False)
    lname = db.Column(db.String(60), nullable=False)
    username = db.Column(db.String(60), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(100), nullable=False)

    books = db.relationship('Book', backref='user')
    projects = db.relationship('Project', backref='user')
    uploads = db.relationship('Upload', backref='user.id')

    #Users Personal Details
    aboutme = db.Column(db.String(450), nullable=False, default="No Description")
    phone = db.Column(db.Text(20), nullable=False, default="00000000000")
    location = db.Column(db.String(60), nullable=False, default="Unknown City")

    # User Accounts
    facebook = db.Column(db.String(100), nullable=True)
    twitter = db.Column(db.String(100), nullable=True)
    linkedin = db.Column(db.String(100), nullable=True)

    #User Education Qualification
    school = db.Column(db.String(100), nullable=True)
    institution = db.Column(db.String(120), nullable=False, default="University")
    course = db.Column(db.String(60), nullable=False, default="Economics")
    fromDate = db.Column(db.DateTime, nullable=True)
    toDate = db.Column(db.DateTime, nullable=True)
    description = db.Column(db.String(160), nullable=True)

    # User Image Filename
    image_file = db.Column(db.String(120), nullable=False, default="default.jpg")

    def __repr__(self):
        return f"User'{self.fname}', '{self.lname}', '{self.username}', '{self.email}', '{self.image_file}'"
1
Moses Eyime Akoji 22 październik 2020, 00:02

1 odpowiedź

Najlepsza odpowiedź

Istnieją pewne problemy z kodem, który może tworzyć ten problem. Dla każdego pola tworzysz dwa punkty wejścia. Na przykład "O mnie" ma dwie pola identyczne ze względu na instrukcję / jeśli. To samo dla wszystkich pól, powiesz wszystko.

Aby złapać błąd, należy naprawdę używać makra. Nie powinieneś dodawać dodatkowego {{Form.fieldName}} do instrukcji ELS / IF.

Usunąłbym je i zobaczyć, czy działa.

W tej chwili, w jaki sposób kolba wie, która pole do użycia, gdy użytkownik się podnosi? Kiedy są dwa pola do wyboru?

0
Matt B 22 październik 2020, 07:05