Creating reusable forms/views in Flask

888 views Asked by At

I am not sure what would be the best route to go down, or I may be missing something obvious.

Example I can give is I have 'person' model and associated form, and view created to add a new 'person'. This all works great. What I would like to do though is use this 'view/form' in a master page with other similar 'views/forms'. With each part being able to add/edit or delete a record from each sub view/form.

So I have all functionality done, just don't know how I can create this master page with child objects, but these child objects can be their own page as well, type of thing.

The idea being that the master page structure is flexible and can accommodate various elements based on the context the user is in.

Should I be looking at blueprints or Jinja2 and its template structure. Or is it how I am handling routes within the main app.

Apologies if this is too vague.

1

There are 1 answers

0
CESCO On

I have done this using AngularJS ng-include directive. You can include whatever html you want, but be careful with Jinja2. If the html you are trying to include contains any script tag it will crash. See my question here. If you need to import a form that needs a script tag you will need to make sure it is not loaded when you are pushing it with Angular. Since it makes a xhr request, you can use flask.request.is_xhr to check if it is angular or the user that is requiring the form. You cannot forget to add this to your angular app Otherwise is_xhr will always return false.

myAppModule.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);

Be careful with base_ templates as well, since they usually load script tags. You can pass the base template through your flask route, and make it extend a blank base html when the request is made through angular. I wish I had my example here, but I am on the bus. Let me know how far you got.