Posts under tag: "Tricks"

How to Implement a notification system in Django

By: summonagus ● at Sept. 16, 2017, 6:54 a.m. ● Posted under: #Tricks, #Django, #Solution

I suggest you to use ContentType to make a dynamic notifications fo any models. This snippet below is an example how to implement the notification system;1. in your models.pyfrom django.db import models from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.fields import GenericForeignKey from django.utils.translation import ugettext_lazy as _ ...

Read More →

Rename app name in django admin

By: summonagus ● at Sept. 14, 2017, 5:45 a.m. ● Posted under: #Tricks, #APP, #Django, #Solution

Prior to Django 1.7 You can give your application a custom name by defining app_label in your model definition. But as django builds the admin page it will hash models by their app_label, so if you want them to appear in one application, you have to define this name in ...

Read More →

Automatic asset versioning in Django

By: summonagus ● at Sept. 6, 2017, 7:36 p.m. ● Posted under: #Tricks, #Django, #Project, #Solution, #Performance

Following on from Ed's "Automatic versioning of CSS, JavaScript and Images" here is a method to version filenames based on modification times to be used in Django as a template tag.This is really handy technique for when you set expires headers to a long way into the future. With headers ...

Read More →

How to launch python Idle from a virtual environment (virtualenv)

By: summonagus ● at Sept. 2, 2017, 5:29 a.m. ● Posted under: #Tricks, #Problem, #Solution

Question from: Khnle - Kevin I have a package that I installed from a virtual environment. If I just launch the python interpreter, that package can be imported just fine. However, if I launch Idle, that package cannot be imported (since it's only available in one particular virtualenv and not ...

Read More →

Optimizing Django ORM Queries for Best Performance

By: summonagus ● at Aug. 25, 2017, 9:34 p.m. ● Posted under: #Tricks, #Problem, #Database, #Django, #Solution

Django framework already comes with lot of ORM (Object Relational Mapping) functionalities. Once the models are created, you can create, update, retrieve and delete objects using its database abstraction api. Any changes in the models can also be handled using migrations which is pretty awesome. Making queries in django is ...

Read More →

Joining each list values with zip

By: summonagus ● at Aug. 25, 2017, 8:10 a.m. ● Posted under: #Tricks, #Looping, #Beginner

Here we use zip to iterate over the two containers in parallel.>>> countries = ['Indonesia', 'India', 'Denmark', 'Singapore'] # 4 length >>> statements = ['is Great', 'is WOW', 'is Good', 'is Awesome'] # 4 length >>> >>> for country, statement in zip(countries, statements): ... print(country, statement) ... Indonesia is Great ...

Read More →

Beautiful Idiomatic Python

By: summonagus ● at Aug. 8, 2017, 6:13 a.m. ● Posted under: #Tricks, #Beginner

Transforming Code into Beautiful, Idiomatic Python: notes from Raymond Hettinger's talk at pycon US 2013. The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have! Original Source: ...

Read More →

Django Count Online Users

By: summonagus ● at Aug. 5, 2017, 4:13 p.m. ● Posted under: #Tricks, #Templates, #Django

Create file where location of is saved, eg: projectname/projectname/ projectname/projectname/ projectname/projectname/ Then following this lines; from django.core.cache import cache from django.conf import settings from django.contrib.auth.models import User from django.utils.deprecation import MiddlewareMixin ONLINE_THRESHOLD = getattr(settings, 'ONLINE_THRESHOLD', 60 * 15) ONLINE_MAX = getattr(settings, 'ONLINE_MAX', 50) def get_online_now(self): return User.objects.filter(id__in=self.online_now_ids or ...

Read More →

Auto rename file after uploaded

By: summonagus ● at Aug. 5, 2017, 8:24 a.m. ● Posted under: #Tricks, #Problem, #Django, #Solution

Django Auto rename file after uploaded. This method to handle duplicated files in same times. This snippet below is how to do that;import os, time, uuid from django.db import models from django.utils.deconstruct import deconstructible class ImageUploader(models.Model): @deconstructible class PathAndRename(object): def __init__(self, sub_path): self.path = sub_path def __call__(self, instance, filename): # ...

Read More →

Django: Custom safe excludes from dangerous XSS Injection

By: summonagus ● at Jan. 3, 2017, 7:44 a.m. ● Posted under: #Tricks, #Templates, #Problem, #Django, #Security, #Solution

Django Custom safe excludes from dangerous XSS Injection. Answered from: from bs4 import BeautifulSoup from django import template from django.utils.html import escape register = template.Library() INVALID_TAGS = ['script', 'style'] def clean_html(value): soup = BeautifulSoup(value) for tag in soup.findAll(True): if in INVALID_TAGS: #tag.hidden = True # you also can ...

Read More →