Flask: detect calling view and redirect accordingly

In a Flask application I've a template showing a list of short notices to the user. I've enclosed this part in a Blueprint called board. The user can click a link to accept a notice. The application records the acceptance to a database and shows the list of notices with the total number of acceptances updated.

THE PROBLEM: the user can also click a notice to see a detailed view of it. He can accept the notice also in the detailed view. The application records the acceptance and shows the same detailed view updated. I want to manage this view in the same Blueprint since most of the code is in common.

WHAT WORKS FINE:

I've two separate endpoints for notices list acceptance and detailed view acceptance.

  • Here's the template for the list "calling" the accept function:
<p> 
   <a class="action" href="">accept</a> 
</p>

and the code taking care of it:

@bp.route("/<int:id>/accept")
@login_required
def accept(id):

    """ Commit acceptance to dbase (omitted) """

    # Redirect to board.index
    return redirect(url_for("board.index"))
  • Here's the template for the detailed view "calling" the view_accept function:
<p>   
   <a class="action" href="">accept</a> 
</p>

and the code taking care of it:

@bp.route("/<int:id>/detailed/accept")
@login_required
def view_accept(id):

    """ Commit acceptance to dbase (omitted) """
    """ Exactly the same as before """

    # Redirect to board.detailed
    return redirect(url_for("board.detailed", id=id))

THE QUESTION: Can I just use one function detecting which template generated the "call" and redirecting to the correct view? Or is the previous approach the correct one? Do I miss something?

request.path and siblings don't seem to work:

@bp.route("/<int:id>/accept")
@bp.route("/<int:id>/detailed/accept")
@login_required
def accept(id):
    """ ...omitted """

    # request.path / request.url_rule / request.endpoint always give the same route
    # (/<int:id>/detailed/accept) regardless of which view generate the call


    # Redirect to board.index or ("board.detailed", id=id) 

Thanks.



from Recent Questions - Stack Overflow https://ift.tt/3jESJSZ
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation