Flask Confirm Action

2024/10/9 23:17:04

I'm creating a site using the Flask framework, and am implementing a confirmation page for (mainly administrative) actions; i.e. deleting a user.

My current method (detailed below) works, but feels quite clunky and seems like a huge amount of work for a simple task. Is there a more optimal solution to this?

Currently I have a route to initiate the action:

@admin.route('/user/<int:user_id>/delete', methods=['GET'])
@login_required
@admin_required
def del_user(user_id):user = User.query.get_or_404(user_id)desc = "delete"subject = user.usernameaction = 'admin.do_del_user'next = url_for('admin.get_user', user_id=user.id)return redirect(url_for('main._confirm', desc=desc, subject=subject, action=action, next=next, user_id=user.id))

Which redirects over to the confirm route:

@main.route('/confirm', methods=['GET', 'POST'])
def _confirm():form = Confirm()kwargs = {}for arg in request.args:if arg != 'action' or arg != 'desc' or arg != 'subject':kwargs[arg] = request.args[arg]action = request.args.get('action')desc = request.args.get('desc')subject = request.args.get('subject')if action is None:abort(404)if form.validate_on_submit():return redirect(url_for(action, confirm=form.confirm.data, **kwargs))return render_template('_confirm.html', form=form, desc=desc, subject=subject)

Which then redirects again to do the actual action after validating the confirmation form:

@admin.route('/user/<int:user_id>/do_delete', methods=['GET'])
@login_required
@admin_required
def do_del_user(user_id):confirm = request.args.get('confirm')next = request.args.get('next')if confirm:user = User.query.get_or_404(user_id)db.session.delete(user)db.session.commit()return redirect(next)

I hope that makes sense! Just to note, desc and subject are passed for the confirmation template, and the kwargs is just to catch anything url_for() needs in building the urls.

Answer

I find this simple answer in a similar question. It has the advantage that can be used with WTforms since you add properties to the form, not the buttons.

<form method="post"onsubmit="return confirm('Are you sure you wish to delete?');">
...
<input type="submit" value="Delete">
</form>
https://en.xdnf.cn/q/69962.html

Related Q&A

Regex for accent insensitive replacement in python

In Python 3, Id like to be able to use re.sub() in an "accent-insensitive" way, as we can do with the re.I flag for case-insensitive substitution.Could be something like a re.IGNOREACCENTS fl…

Python + Flask REST API, how to convert data keys between camelcase and snakecase?

I am learning Python, and coding simple REST API using Flask micro-framework.I am using SQLAlchemy for Object-relational-mapping and Marshmallow for Object-serialization/deserialization.I am using snak…

pytest reports too much on assert failures

Is there a way for pytest to only output a single line assert errors?This problem arises when you have modules with asserts, If those asserts fails, it dumps the entire function that failed the assert…

pulp.solvers.PulpSolverError: PuLP: cannot execute glpsol.exe

I am a newbie with python and optimization. I am getting some error, please help me resolve it. I tried running the below mentioned code in PyCharm where I am running Anaconda 3from pulp import * x = L…

Django urldecode in template file

is there any way do the urldecode in Django template file? Just opposite to urlencode or escapeI want to convert app%20llc to app llc

Structure accessible by attribute name or index options

I am very new to Python, and trying to figure out how to create an object that has values that are accessible either by attribute name, or by index. For example, the way os.stat() returns a stat_resul…

Loading data from Yahoo! Finance with pandas

I am working my way through Wes McKinneys book Python For Data Analysis and on page 139 under Correlation and Covariance, I am getting an error when I try to run his code to obtain data from Yahoo! Fin…

Run Multiple Instances of ChromeDriver

Using selenium and python I have several tests that need to run in parallel. To avoid using the same browser I added the parameter of using a specific profile directory and user data (see below). The p…

numpy 2d array max/argmax

I have a numpy matrix:>>> A = np.matrix(1 2 3; 5 1 6; 9 4 2) >>> A matrix([[1, 2, 3],[5, 1, 6],[9, 4, 2]])Id like to get the index of the maximum value in each row along with the valu…

How do I add a python script to the startup registry?

Im trying to make my python script run upon startup but I get the error message windowserror access denied, but I should be able to make programs start upon boot because teamviewer ( a third-party prog…