Article From:

Learning “Flask Web development” book, I want to increase the function of delete blog articles. The corresponding code is modified.

def delete_post(id):
    post = Post.query.get_or_404(id)
    return redirect(url_for('._posts',
                             page=request.args.get('page', 1, type=int)))


          <a class="btn btn-danger btn-xs" href="{{ url_for('.delete_post',, page=page) }}">Delete</a>


<ul class="posts">
    {% for post in posts %}
    <li class="post">
        <div class="post-thumbnail">
            <a href="{{ url_for('.user', }}">
                <img class="img-rounded profile-thumbnail" src="{{ }}">
        <div class="post-content">
            <div class="post-date">{{ moment(post.timestamp).fromNow() }}</div>
            <div class="post-author"><a href="{{ url_for('.user', }}">{{ }}</a></div>
            <div class="post-bdoy">
                {% if post.body_html %}
                    {{ post.body_html | safe }}
                {% else %}
                    {{ post.body }}
                {% endif %}
            <div class="post-footer">
                {% if current_user == %}
                <a href="{{ url_for('.edit', }}">
                    <span class="label label-primary">Edit</span>
                {% elif current_user.is_administrator %}
                <a href="{{ url_for('.edit', }}">
                    <span class="label label-danger">Edit [Admin]</span>
                {% endif %}
                <a href="{{ url_for('.post', }}">
                    <span class="label label-default">Permalink</span>
                <a href="{{ url_for('.post', }}#comments">
                    <span class="label label-primary">{{ post.comments.count() }}Comments</span>
                <a class="btn btn-danger btn-xs" href="{{ url_for('.delete_post',, page=page) }}">Delete</a>

    {% endfor %}

@main.route('/post/<int:id>', methods=['GET','POST'])
def post(id):
    post = Post.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(,
        flash('Your comment has been published.')
        return redirect(url_for('.post',, page=-1))
    page = request.args.get('page', 1, type=int)
    if page == -1:
        page = (post.comments.count() - 1) // \
            current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1
    pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(
        page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
    comments = pagination.items
    return render_template('post.html', posts=[post], form=form,
                            comments=comments, pagination=pagination)


{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "_macros.html" as macros %}

{% block title %}Flasky - Post{% endblock %}

{% block page_content %}
{% include '_posts.html' %}
<h4 id="comments">Comments</h4>
{% if current_user.can(Permission.COMMENT) %}
<div class="comment-form">
    {{ wtf.quick_form(form) }}
{% endif %}
{% include '_comments.html' %}
{% if pagination %}
<div class="pagination">
    {{ macros.pagination_widget(pagination, '.post', fragment='#comments', id=posts[0].id) }}
{% endif %}
{% endblock %}

After running, the redirection fails when you click the delete button, presumably because _posts.html is used as a local template and the URL is not the address. Although the redirection address can be changed to index. But the article shows two pages on index and user. suchNo matter where to delete blog posts, it can only be redirected to index pages.

Ask, in this case. After page operation, how do I redirect to the current page??

Answer 0:
return redirect(url_for('._posts',
                             page=request.args.get('page', 1, type=int)))

I hope you can make it._postsThe view function is written out.

The following is a positive answer

Here, I understand that your intention is to achieve blog content.Increase or decrease and change inspectionFunction, which is commonly usedCRUD Function. However, when we realize the deletion of blog content, this function,You have a problem in your mind

First of all, your idea is to use URL to locate resources and to delete operations. This idea is very restful (as to how to implement restful, please continue to read the book), but in this front and back flask architecture, you use it in the actual project.Jinja2 uses bootstrap more than flask-wtf. Then, your restful thought is impossible.

In order to better solve your problem, I will first follow your wrong thoughts to smooth out the problem.

You want to use itDelete the function of URL —— /post/delete/<int:id>,Try to use a button<a class="btn btn-danger btn-xs" href="{{ url_for('.delete_post',, page=page) }}">Delete</a> Delete To jump to the URL with the delete function (this page will not be displayed at all), and to delete the actual action of the content of the blog after the completion, then jump back to the original blog article page. But yoursJump back to the original pageRedirection is the content of this –return redirect(url_for('._posts', page=request.args.get('page', 1, type=int))),Yours._postTo make the view function there, it is obvious that the idea is here, it can not be remedied, do not know how to jump back to the original page.

As for this “Redirects to a URL with operations, such as adding or changing, and does not need to display. Once the operation is completed, it will return to the original page.”The function of HTTP protocol is not supported. Why? I don’t know, please.To make up more lessons。If you want to use this feature, JavaScript will still be able to satisfy you, using flask to specialize in restful apis, JavaScript to handle the front end.

How to implement the article in the framework of flask integration?Increase or decrease and change inspection What about the function?

The answer is to use more than one form in a page.

You can refer to the view function in the admin blueprint of my pyblog

In the past two days, I am writing an article based on the flask framework, which is implemented by flask-wtf.Increase or decrease and change inspectionThe feature article, which I’ve written today, is writing text, and I’ll publish it later in the technical circle I’ve created uuuuuuuuuuuflaskDiscussion group Or in my blog, lotus root space, please look forward to it.

Answer 1:

redirect Is redirected to view function instead of webpage template.
The problem is in the _posts function. Check whether the function exists or paste it out.

Leave a Reply

Your email address will not be published. Required fields are marked *