Django Generate Unique Slug
Generate unique slug refference by another field.
- django
- slug
- slugify
- django-slug
Generate unique slug refference by another field.
This is an example of Django auth with JWT tokens, you can find how to add [jwt auth to Django Rest Framework in this tutorial](https://www.techiediaries.com/django-rest-framework-jwt-tutorial/)
Small snippet that will resize all images before they uploaded to the server.
# Django AJAX Form View ### A simple example for an AJAX-powered view
Dynamic Paginator Mixin for Django 1.8.* - 1.9.*, also work for CBV (Class Bassed View) but not for "django generic view".
Fixed minimal version, works with Django 1.7+, tested on Django 1.9. Add the following to your settings: AUTHENTICATION_BACKENDS = [ 'project.backends.UserModelEmailBackend', # Login w/ email 'django.contrib.auth.backends.ModelBackend', # Login w/ username ]
Disables all editing capabilities in admin and display all fields as read-only.
This snippets generate the sum of the field values, for use in summary reports. More info in https://github.com/thomazs/django_templates_plus
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.
You can use this as a model method.
This is a simple example of feeding multiple Forms into a single Form via its constructor method, to work with a single FormView and reap the benefits of Django's awesome Form validation system. It's a Form class that defines (or takes via an argument to its constructor) *parent* Forms (that can, for instance, be ModelForms, to take advantage of the automatic Field generation) and takes its fields from there. An advanced user won't be impressed by this, so excuse if this snippet is out of place, but a rather inexperienced user such as myself might find it interesting and make him willing to explore Django's internals a bit more.
Start simple SMTP server on localhost:25 and print to standard output all email headers and the email body. Useful for debugging outgoing mail without configuring SMTP daemon in development enviroment.
Note: must call within __init__() method, so you must do self.fields["field"] = ModelChoiseField(...). This is because I did not use a ModelChoiceIterator. A subclass of ModelChoiceField which represents the tree level of each node when generating option labels. It's limited to one level of nesting, if you need more, you should consider the django-mptt package. For example, where a form which used a ModelChoiceField: category = ModelChoiceField(queryset=Category.objects.all()) ...would result in a select with the following options: --------- Root 1 Root 2 Child 1.1 Child 1.2 Child 2.1 Using a NestedModelChoiceField instead: category = NestedModelChoiceField(queryset=Category.objects.all(), related_name='category_set', parent_field='parent_id', label_field='title') ...would result in a select with the following options: Root 1 --- Child 1.1 --- Child 1.2 Root 2 --- Child 2.1
jQuery dataTables is a fantastic and powerful client-side plugin, which has many capabilities. There are many libraries out there that easily integrate it with django with little to no effort. However, they provide everything out of the box, which isn't always the most flexible solution. I also think beginners have a lot to learn from not using already created tools. So, finding the general examples out there lacking, I though I'd hatch out a quick example of how to very basically integrate the two. I supply two lists at the top which define how to order the fields and which are searchable (quite similar to the one defined in the admin site) and later everything is figured from there. Of course, for a little more complex implementation (say using a method instead of a field) this will not work (since getattr doesn't automatically call a function if it is a function), but this snippet isn't supposed to provide everything but a very basic usage example, which can be very quickly expended in any way needed. Anyway, this snippet should work on all django versions and all dataTables versions without a hitch. last note- I use `cStringIO` with `json.dump` out of good personal experience with that settings, though it might not be the best way to serialize the data. Good luck, share use and enjoy, ~yuvi
Sometimes you have context variables that are needed on many pages in a site, but not all. You only want them to be evaluated when actually needed, especially if they are expensive calculations that do DB queries etc. The pattern to use is shown: put a callable into the context, not the final value, and also wrap the callable with memoize_nullary.
3110 snippets posted so far.