PreviewMixin adds a preview page for Django's CBV (FormView, UpdateView, CreateView). After a form has been submitted, it is returned again, optionally in a different template to confirm. If the form is submitted with the same data, the default "form_valid" function is executed.
Features:
1. `process_preview` - function executed after submitting the form for the first time (default is to do nothing)
2. `done` - function for the action if the confirm page is sent (defaults to whatever form_valid of the django cbv does)
3. `preview_template_name` - variable with the name of the template for the confirm page (defaults to taking the same template as for the initial form)
4. new function `security_hash` to calculate a hash which is added to the confirmation form.
Works kind of like django-formtools, just as a Mixin for the default Django cbv.
The recipe uses deferred constraint validation to create circular references across database tables. The example requires Postgres (MySQL doesn't support deferred constraints).
To achieve this, the following is required:
* Insertions must be performed in a transaction. Foreign key constraints will be validated at the end of the transactions, allowing for insertion of rows with FKs pointing to rows that don't exist yet.
* Primary keys need to be generated before insertion. That's what `prefetch_id` does by pulling the next value from the `*_id_seq` sequence.
You can test how your django app behaves depending on what kind of response it gets from the API. It assumes were're using the python requests library.
I am used to load forms directly into modals using dajax but I found out I had to load the scripts using an ajax call from the browser.
You can see here an example of a dynamically loaded form and the function used to load the scripts.
For several projects I am using generic views instead of django-admin, I needed a generic view for constance instead of using their django-admin based app.
Obfuscate javascript strings.
* Convert
* `{{'Pita Hummus'|js_obfuscate_string}}`
* to something like
* `'P4itzga Hnumg7mubs'.replace(/[4zgn7b]/g,'')`
Making it hard to text-search for strings in your code.
I've reimplemented the code I found somewhere on the web within my models file. The earlier version was incapable of converting all formats to JPG while this code converts all formats and compresses all of them successfully.
## required
* `{% load trans%}`before using this snippets
* Add this [template filter](https://djangosnippets.org/snippets/2253/) to your custom templatetags and load it before using this snippets
* Bootstrap framework
Decorating a whole view involves overriding the dispatch method so you can decorate it, even if all you do is a passthrough. This creates a class decorator for decorating CBVs. For example, if you want to make a view require a login:
@decorate_dispatch(login_required)
class MyCBV(TemplateView):
...
A "fake" model field which is an alias to another underlying database field.
Mirrors everything including queryset lookups, instance attributes, even if the field has been used on the model already (this isn't possible just by using db_column).
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.