Flask wtforms Дата и время, средство выбора доступно для новой записи, но не для обновления, несмотря на тот же шаблон - PullRequest
0 голосов
/ 04 мая 2020

Я могу создать запись даты и времени для новой записи, которая успешно сохраняется. Однако виджет ввода даты и времени недоступен, когда я хочу обновить и внести изменения. Я не понимаю почему. Новый и обновленный маршрут используют один и тот же шаблон crete_post. html. Я включил этот шаблон ,ways.py и forms.py ниже.

Любой совет будет высоко ценится. enter image description here

enter image description here

create_post. html

...

<div class="form-group">
                    <h5> Date and Time</h5>
                    {% if form.date_posted.errors %}
                        {{ form.date_posted(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.date_posted.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.date_posted }}
                    {% endif %}

                    {% if form.time_posted.errors %}
                        {{ form.time_posted(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.time_posted.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.time_posted }}
                    {% endif %}
                </div>
...

rout.py

...

@app.route("/post/new", methods=['GET', 'POST'])
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    description=form.description.data,
                    status=form.status.data,
                    priority=form.priority.data,
                    date_posted=form.date_posted.data,
                    time_posted=form.time_posted.data,
                    assigned_to=form.assigned_to.data,
                    reported_by=form.reported_by.data)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been created!', 'success')
        return redirect(url_for('home'))
    return render_template('create_post.html', title='New Post', form=form,
                legend='New Post')

@app.route("/post/<int:post_id>")
def post(post_id):
    post = Post.query.get_or_404(post_id)
    return render_template('post.html', title=post.title, post=post)

@app.route("/post/<int:post_id>/update", methods=['GET', 'POST'])
# @login_required
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.description = form.description.data
        post.date_posted=form.date_posted.data,
        post.time_posted=form.time_posted.data,
        post.status = form.status.data
        post.priority = form.priority.data
        post.assigned_to = form.assigned_to.data
        post.reported_by = form.reported_by.data
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('home', post_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.description.data = post.description
        form.date_posted = post.date_posted
        form.time_posted = post.time_posted
        form.status.data = post.status
        form.priority.data = post.priority
        form.assigned_to.data = post.assigned_to
        form.reported_by.data = post.reported_by
    return render_template('create_post.html', title='Update Post',
            form=form, legend ='Update Post')

forms.py

status_options = [('ongoing', 'ongoing'), ('completed', 'completed')]
priority_options = [('moderate', 'moderate'),('high', 'high'),('low','low')]
class PostForm(FlaskForm):
    title = StringField('Title', validators=[DataRequired()])
    description = TextAreaField('Description')
    date_posted  = DateField('Date')
    time_posted  = TimeField('Time')
    status = SelectField('Status',choices = status_options)
    priority = SelectField('Priority',choices = priority_options)
    assigned_to = StringField('Assigned To')
    reported_by = StringField('Reported By')
    submit = SubmitField('Save')

1 Ответ

0 голосов
/ 04 мая 2020

В этом:

elif request.method == 'GET':
    form.title.data = post.title
    form.description.data = post.description
    form.date_posted = post.date_posted
    form.time_posted = post.time_posted
    form.status.data = post.status
    form.priority.data = post.priority
    form.assigned_to.data = post.assigned_to
    form.reported_by.data = post.reported_by
return render_template('create_post.html', title='Update Post',
        form=form, legend ='Update Post')

". Data" отсутствует в form.date_posted и form.time_posted. Измените его, как показано ниже:

    form.date_posted.data = post.date_posted
    form.time_posted.data = post.time_posted
...