Two template tag filters that can be used to create a table from a sequence.
<table>
{% for row in object_list|groupby_columns:3 %}
<tr>
{% for obj in row %}
<td>{{ obj }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
The example above would create a table where items read from top to bottom, left to right, in 3 columns. "Empty" cells are added to the sequence by the filter so that your rows balance.
This is an extension of the fantastic snippet "Compact list_filter" written by **onlinehero**. Follow his instructions for the installation.
My version add the number of filtered objects beside the label of the filters defined by the list_filter property.
This will give you a <input type='number'> field.
This is helpful when you want to use HTML5 for newer browsers. Older browsers will just interpret this as <input type='text'>
This is an improvement of [joshua](http://djangosnippets.org/users/joshua/)'s [SQL Log Middleware](http://djangosnippets.org/snippets/61/).
If you have more than one database connection, then all queries are logged, grouped by connection.
If a connection has no queries, then it's not shown.
Method which gets a chunk of HTML to perform standard Google Analytics tracking as well as a noscript version. Very useful for finding out what percentage of your users do not have JavaScript support.
This is Python port of the PHP example here: [http://andrescholten.net/google-analytics-zonder-javascript/](http://andrescholten.net/google-analytics-zonder-javascript/)
To use, you need to put the following into your settings file:
GOOGLE_ANALYTICS_ID = 'UA-1234567-1'
GOOGLE_ANALYTICS_DOMAIN = 'example.com'
Based on [http://djangosnippets.org/snippets/1615/](http://djangosnippets.org/snippets/1615/)
Added tweets cache. Cache time is configurable through project settings: TWITTER_TIMEOUT=300. Default time is 300 seconds = 5 minutes.
This improve performance by reducing the twitter api queries interval.
Due to compliance requirements in the financials industry we needed to log every request a user made to our system, the action taken (view function) and response from the server.
I found a lot of other logging solution bit most revolved around debugging and DB query logging. I needed to be able to tell what a user did while being logged in as much detail as I could with out tracking the mouse pointer position on screen.
So I created this (, *my first* ,) middleware. Its very simple really. keeping track of a request, view_func and response object in a single model called Record (models.py file included in the code).
The fields I used are optimized to what I intend to show in the UI I am planning for this model. Depending on how you use the doc string of your views they can be tapped to explain to the user what each request/func/responce group in a session is meant to do.
There were a few gotcha's:
1. I only care about authenticated requests. So I added the 'quest.user.is_authenticated()' test.
2. I did not care about the favicon request so I skipped them.
2. The actual login request is not authenticated while the response is. This caused the process_response/view to look for a record that is not there. So I added the 'except ObjectDoesNotExist' to skip this case.
I added one bell: Logging a full HTML reply is wasteful and mostly useless. I added two values in the setting files. LOGALL_LOG_HTML_RESPONSE to toggle if we want to log them or not. And LOGALL_HTML_START to describe what a full HTML starts with. Personally I use the first few characters of my base.html template that all the rest of my templates expend. I simplified the code to the left for readability.
Django 1.3 has an assertNumQueries method which will allows you to simply specify the number of queries you expect. Sometimes, however, specifying the exact number of queries is overkill, and makes the test too brittle. This code provides a way to make more forgiving tests.
See http://lukeplant.me.uk/blog/posts/fuzzy-testing-with-assertnumqueries/
I basically mixed both BRCPFField and BRCNPJField to create a widget that validates either an CPF or an CNPJ. The doc strings are not localized. So you probably have to hardcode it yourself.
This class tracks changes in Django Admin. When a save action is performed, it stores the value of the old object using the Memento model.
Example of code for a model in **admin.py** for a custom 'app':
from app.memento.models import Memento
def save_model(self, request, obj, form, change):
obj.save()
data = serializers.serialize("json", [obj, ])
m = Memento(app="Unidade",model=modelName,data=data, user=request.user)
m.save()
class UnidadeAdmin(admin.ModelAdmin):
pass
UnidadeAdmin.save_model = save_model
This stores the former values of 'Unidade' model on 'Memento' model data.
Not tested on previous versions of Django, but could work on them too.
Uses the token generator located at django.contrib.auth.tokens as an authentication mechanism aimed mainly at API calls. Any POST request with a valid token and user parameter will work as if the user were logged in normally.
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.