Skip to content

Instantly share code, notes, and snippets.

@victorono
victorono / urls.py
Created February 10, 2014 19:24
Robots in django
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.views.generic import TemplateView
urlpatterns += patterns('',
url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This I found years ago, floating around django snippets.
# If anyone knows the proper attribution, please email me
# at steven@agoodclooud.com, and I'll update t!
#
import re
from types import UnicodeType
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter(name='highlight')
def highlight(text, word):
return mark_safe(text.replace(word, "<span class='highlight'>%s</span>" % word))
"""
import logging
logging.basicConfig(filename='db.log')
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
@victorono
victorono / .htaccess
Created January 14, 2014 22:11
Index for IE
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "MSIE [6-8]" [NC]
# RewriteRule (.*)\.html index.IE.html
RewriteRule index\.html index.IE.html
DirectoryIndex index.html
</IfModule>
@victorono
victorono / twitter_extras.py
Created January 12, 2014 15:49
filter tweet django templatetag
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
from django.utils.safestring import mark_safe
from django import template
register = template.Library()
@register.filter(name='twitter_tags')
@victorono
victorono / decorator_json.py
Last active January 2, 2016 21:09
return json from view django
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from functools import wraps
from django.http import HttpResponse
def render_to_json(**jsonargs):
"""
Renders a JSON response with a given returned instance. Assumes json.dumps() can
class BaseObject(models.Model):
id = models.AutoField(primary_key=True)
creation_datetime = models.DateTimeField(auto_now_add=True)
update_datetime = models.DateTimeField(auto_now=True)
class News(BaseObject):
class Meta:
managed = True
db_table = 'news'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.cache import cache
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
help = 'Fully clear your site-wide cache.'
@victorono
victorono / forms.py
Created December 29, 2013 17:30
Apply error class to form elements django
class MyForm(forms.Form):
"""
Extend from this form so your widgets have an 'error' class if there's
an error on the field.
"""
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
if self.errors:
for f_name in self.fields:
if f_name in self.errors: