Posts under tag: "Templates"

Django Count Online Users

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

Create file middleware.py where location of settings.py is saved, eg: projectname/projectname/__init__.py projectname/projectname/middleware.py projectname/projectname/settings.py 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 →

Django: Custom safe excludes from dangerous XSS Injection

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

Django Custom safe excludes from dangerous XSS Injection. Answered from: http://stackoverflow.com/a/41434870/6396981 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 tag.name in INVALID_TAGS: #tag.hidden = True # you also can ...

Read More →

How to custom html choose image upload for django markdownx

By: summonagus ● at Dec. 19, 2016, 9:38 p.m. ● Posted under: #Templates, #Tricks, #Solution, #Django, #Ajax, #Problem

Django markdownx is a Markdown editor built for Django https://github.com/adi-/django-markdownx Previously i already created isssue before: https://github.com/adi-/django-markdownx/issues/36, as the owner says: There is no easy way to do it "quick", I think. You could change JS script a bit to be able to send images. This should be a simple ...

Read More →

Implement Realtime Search using Django and Ajax

By: summonagus ● at Nov. 13, 2016, 12:17 a.m. ● Posted under: #Templates, #Tricks, #Solution, #Django, #Ajax

Hello guys and welcome back, this night i want to share a simply tutorial how to Implement Realtime Search using Django and Ajax. Lets goo... ^_^1. yourapp/models.pyfrom django.db import models class Post(models.Model): title = models.CharField(max_length=200) description = models.TextField() 2. yourapp/views.pyfrom django.shortcuts import render from django.db.models import Q from yourapp.models import ...

Read More →

How to minify HTML output in Django

By: summonagus ● at Oct. 2, 2016, 4:40 p.m. ● Posted under: #Tricks, #Templates, #Django

So, you want to compress the size of the HTML that Django generates? Here is the Django way to do it. In order to do this, you'll have to write a middleware for your application. So, create a file called middleware.py (for this example, but you can name it whatever ...

Read More →

Django: Remove Duplicates Filter for Related Posts

By: summonagus ● at Oct. 2, 2016, 3:28 p.m. ● Posted under: #Templates, #Django, #Looping

Django: Remove Duplicates Filter for Related Posts. 1. In yourapp/models.py class Post(models.Model): title = models.CharField(max_length=200) slug = models.SlugField(max_length=200, unique=True) tags = models.ManyToManyField('Tag') 2. In yourapp/views.py class DetailPostView(generic.DetailView): model = Post template_name = 'blog/blog_detail.html' def get_context_data(self, **kwargs): context_data = super(DetailPostView, self).get_context_data(**kwargs) related_posts = Post.objects.filter( tags__in=list(self.object.tags.all()) ).exclude(id=self.object.id).distinct() context_data['related_posts'] = related_posts[:5] # limit ...

Read More →

Django: Simple User Authentication Login and Logout

By: summonagus ● at Oct. 2, 2016, 8:13 a.m. ● Posted under: #Templates, #Problem, #Django, #Security

Django: Simple User Authentication Login and Logout. This problem hasbeen asked with someone in in https://www.facebook.com/groups/DjangoID/, he asked "how to custom template of login form in django". First, makesure you understrand how django is work. and in this method, we used default model User from django. and default user authentications from django. There just modified ...

Read More →

How to add class active for html templates, eg: nav menus, bassed in generic.ListView

By: summonagus ● at Oct. 2, 2016, 8:13 a.m. ● Posted under: #Tricks, #Templates, #Problem, #Django

How to add class active for html templates, eg: nav menus, bassed in generic.ListView. This my problem that was asked in gitter of django https://gitter.im/django/django?at=569019e287cb99b53b8802e8, and this is my question of it: somebody can help me how to add class active for html templates, eg: nav menus, bassed in generic.ListView? some ...

Read More →

Django: Dinamic path url by name in templates

By: summonagus ● at Oct. 2, 2016, 8:13 a.m. ● Posted under: #Templates, #Problem, #Django

Hello there, this time i just wanna show how to use a dinamic path url in templates django. Before it, this hasbeen asked and aswered by someone in stackoverflow. There is simply how to manage it, and usefull for templates django. 1. In your urls.py from django.conf.urls import include, url ...

Read More →

New feature for output post with json format for Django Blog Python Learning v2.1.1

By: summonagus ● at Oct. 2, 2016, 8:13 a.m. ● Posted under: #Templates, #Django, #News, #Project

New feature for output post with json format for Django Blog Python Learning v2.1.1 1. Add new function in your blog/models.py to outputing post with json format. (for complete script, checkout this: https://github.com/agusmakmun/Django-Blog-Python-Learning-v.2/commit/96657b82ab1dd48c74bfdf9c1af2bb12e8b07f6d) def json_default_post(self): domain = 'https://python.web.id' cover_url = '' if self.cover.url: cover_url = self.cover.url else: cover_url = cover_url return dict( ...

Read More →