Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-Admin - Customizing views

I'm developing a web-app using Flask and pyMongo, and I've recently started to integrate the Flask-Admin module (1.0.4), given the fresh mongodb support.

All is smooth and fine when using ModelViews, but when it comes to subclassing a BaseView I simply can't get it working.

Here is my setup:


user_view = Admin(app, name='User stuff', url="/user", endpoint="user")


class ProfileForm(wtf.Form):
    username = wtf.TextField('Username', [wtf.Required()])
    name = wtf.TextField('Name', [wtf.Required()])


class Profile(BaseView):
    @expose('/', methods=('GET', 'POST'))
    def profile(self):
        user = User(uid) # gets the user's data from DB
        form = ProfileForm(request.form, obj=user)
        if form.validate_on_submit():
            data = form.data
            user.set(**data)
            user.save()
            flash("Your profile has been saved")
        else:
            flash("form did not validate on submit")
        return self.render('user/profile.html', form=form, data=user)

user_view.add_view(Profile(name='Profile', url='profile'))

When submitting the form, wtforms does not report any error (unless there is any) but the validation does not return to my profile view (the else: branch is always executed)

There is no way I could find to make this work, inspite having thoroughly scanned flask-admin documentation, source code and examples.

Could anybody suggest how I could fix my code, or work around this problem ?

like image 339
user1981924 Avatar asked Jan 15 '13 23:01

user1981924


People also ask

How do I create an empty admin interface in flask?

The first step is to initialize an empty admin interface for your Flask app: Here, both the name and template_mode parameters are optional. Alternatively, you could use the init_app () method. If you start this application and navigate to http://localhost:5000/admin/, you should see an empty page with a navigation bar on top.

How do I override the default templates in flask-security?

Flask-Security is packaged with a default template for each view it presents to a user. Templates are located within a subfolder named security. The following is a list of view templates: Overriding these templates is simple: Create a folder named security within your application’s templates folder

What is flask and why should I use it?

As a micro-framework, Flask lets you build web services with very little overhead. It offers freedom for you, the designer, to implement your project in a way that suits your particular application. Why Flask-Admin?

What are flask-Security’s views?

Flask-Security bootstraps your application with various views for handling its configured features to get you up and running as quickly as possible. However, you’ll probably want to change the way these views look to be more in line with your application’s visual design.


1 Answers

I have suspicion that form is getting submitted using GET method instead of POST or Flask-WTF CSRF check fails.

Here's small gist I made with your sample code. It works as expected: https://gist.github.com/4556210

Few comments:

  1. Template uses some Flask-Admin library functions to render the form. You don't have to use them if you dont want to;
  2. Uses mock user object
  3. Put template under templates/ subdirectory if you want to run the sample.

In either case, Flask-Admin views behave exactly same way like "normal" Flask views, they're just organised differently.

like image 116
Joes Avatar answered Oct 07 '22 10:10

Joes