Login

All snippets

Snippet List

LoginRequiredMiddleware

### settings.py MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'utils.LoginRequiredMiddleware', ] LOGIN_REQUIRED_URLS = [ r'^panel/(.*)$' ] this will help any url under `panel/` require login.

  • middleware
  • authentication
  • login_required
Read More

spaceless_json

Now you can format and compress json-data in django template

  • django
  • templatetag
  • json
  • spaceless
  • formatting
  • application/id+json
Read More

Tweet embed template tag

Takes a tweet url, requests the json from Twitter oEmbed, parses the json for the html element and returns it to your template. The html returned is ready to go and will be shown as a tweet on your web page. This uses the Requests library for Python. A full example can be found on GitHub https://github.com/z3ke1r/django-tweet-embed.

  • django
  • templatetag
  • json
  • embed
  • twitter
  • parse
  • requests
  • tweet
Read More

Bootstrap Accordian

<p>This is an example of Leamug web development, you can find how to add <a href="https://starttemplates.com/product/bootstrap-accordion/">start templates designer</a></p> Collapsible accordion are useful when you want to hide and show large amount of content. If you are looking for a modern, flat and professionally designed Bootstrap accordion, then we have a best choice for you called Trendy css build collapse accordion with plus and minus icons which can be used in your all types of websites for <a href="https://hindilife.in/majedar-masti-bhare-funny-chutkule-hindi-mein/">Latest Majedar Jokes</a> The Bootstrap accordion with four panels is created. The main div is assigned the panel-group class. The role data attribute is set as tab list while aria-multivariable is set as true. The panel with content div is assigned the panel and panel-default classes while its inner div contains the classes for panel heading, title, and content of the panel. The div showing the panel content is given the panel-body class along with collapse and panel-collapse classes. It will make the panel body collapsed as web page loads.

  • Responsive
  • Bootstrap
  • Web design
Read More

Hand over model instance relations to another instance of the same model

If you have a model instance you want to merge into another, it's handy to hand over all the relations into the model you want to merge into, so the deletion won't trigger cascading deletions from other tables. You can pass an `Iterable` of the same objects (a.k.a `QuerySet`) to the model, and then process it with the new model's `pk`.

  • foreignkey
  • many2many
  • relations
  • handover
Read More

Auth decorators with 403 - Django 1.11

This decorator is based on user_passes_test, but when a user is logged in and fails the test, it will render a 403 error instead of redirecting to login - only anonymous users will be asked to login. Version working with Django 1.11, based on version found on [https://djangosnippets.org/snippets/254/] , which is deprecated.

  • auth
  • user_passes_test
  • 403 permission_required
Read More

DRF - Optimizing ModelViewSet queries

Using Django REST Framework for Model views there is always the issue of making duplicated queries without either prefetching the objects that will be accessed using the serializer and as such will lead to large number of queries being made to the database. This will help in optimizing the queryset used for the viewset by accessing the `_meta.fields` property of the serializer.

  • django
  • rest
  • rest-api
  • django-rest-framework
Read More

Support disabling options in a select widget

Provide the ability to disable a select option with Django2. This can be done during the widget initialization (i.e when the widget field is created) or during form initialization. This will disable the select option based on a context or by specifying the values that should be disabled.

  • widgets
  • django-forms
Read More

3109 snippets posted so far.