Django Important Stuff

Django custom user email account verification:

https://stackoverflow.com/questions/24935271/django-custom-user-email-account-verification

Django User Agents:

A django package that allows easy identification of visitor's browser, OS and device information, including whether the visitor uses a mobile phone, tablet or a touch capable device. Under the hood, it uses user-agents.
from django.core import serializers
from django.shortcuts HttpResponse

def service_type(request):
    obj = ServiceType.objects.all()
    data = serializers.serialize('json',obj)
    return HttpResponse(data,content_type="application/json")

resources:
https://docs.djangoproject.com/en/dev/topics/serialization/
https://stackoverflow.com/questions/16790375/django-object-is-not-json-serializable



Custom template tags and filters

https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

from django import template
register = template.Library()

@register.filter
def index(indexable, i):

    return indexable[i]

Django - iterate number in for loop of a template

Django provides it. You can use either:
  • {{ forloop.counter }} index starts at 1.
  • {{ forloop.counter0 }} index starts at 0.
In template, you can do:
{% for item in item_list %}
    {{ forloop.counter }} # starting index 1
    {{ forloop.counter0 }} # starting index 0

    # do your stuff
{% endfor %}

How to delete files from filesystem using post_delete - Django 1.8

import os
from django.db import models

def _delete_file(path):
   """ Deletes file from filesystem. """
   if os.path.isfile(path):
       os.remove(path)

@receiver(models.signals.post_delete, sender=ProductImage)
def delete_file(sender, instance, *args, **kwargs):
    """ Deletes image files on `post_delete` """
    if instance.image:
        _delete_file(instance.image.path)

@receiver(models.signals.post_delete, sender=Product)
def delete_file(sender, instance, *args, **kwargs):
    """ Deletes thumbnail files on `post_delete` """
    if instance.thumbnail:
        _delete_file(instance.thumbnail.path)

Comments

Popular posts from this blog

Generate SHA-1 and SHA-256 for Debug and Release android app

Transfer file to Server and vice versa

How to Import and Export MySQL Database Command line in Linux