For a model FK'd to a user or profile, you might want every instance of that thing to appear in your site and in the admin as say "Joe's bucketlist." But if Joe's name is Ross, you want "Ross' bucketlist", not "Ross's bucketlist."
This field is similar to the standard URLField, except it checks the given URL for a HTTP 301 response (permanent redirect) and updates its value accordingly.
For example:
>>> url = RedirectedURLField()
>>> url.clean('http://www.twitter.com/')
>>> 'http://twitter.com/'
In models:
class TestModel(models.Model):
url1 = RedirectedURLField('Redirected URL')
url2 = models.URLField('Standard URL')
Accepts the same arguments as the 'range' builtin and creates
a list containing the result of 'range'.
Syntax:
{% mkrange [start,] stop[, step] as context_name %}
For example:
{% mkrange 5 10 2 as some_range %}
{% for i in some_range %}
{{ i }}: Something I want to repeat\n
{% endfor %}
Produces:
5: Something I want to repeat
7: Something I want to repeat
9: Something I want to repeat
A general AntiSpamForm using some tricks to prevent spam based on current [django.contrib.comments.forms](http://code.djangoproject.com/browser/django/trunk/django/contrib/comments/forms.py). It uses a timestamp, a security hash and a honeypot field. See [AntiSpamModelForm](http://www.djangosnippets.org/snippets/1856/) too.
A simple template tag that returns the favicon URL for a given arbitrary URL.
Put the code into a python module in the templatetags package of a Django app (e.g. myapp/templatetags/mytags.py), and use it like this:
{% load mytags %}
...
<img src="{% favicon posting.url %}/>
<a href="{{ posting.url }}">{{posting.title}}</a>
...
Decorator for views that need confirmation page. For example, delete
object view. Decorated view renders confirmation page defined by template
'template_name'. If request.POST contains confirmation key, defined
by 'key' parameter, then original view is executed.
Context for confirmation page is created by function 'context_creator',
which accepts same arguments as decorated view.
I never found a good snippet or tutorial to get the app_list (in django.admin) on other pages except the index page. So i started asking on irc j00bar has given me a very nice answer, but first i didn't know what to do with it till now.
Anyways this snippet is very handy for the people who wants this but don't know how to get it.
This is special made for the django version 1.1
Installation is quite easy, it is a context processor, so download this file put anywhere in your project, i got it in a app called cms_theme (theme and template related stuff.) and put the location in your settings.py file, example:
`TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.request',
'****cms.cms_themes.context_processors.theme',
'****cms.cms_themes.context_processors.applist',
)`
The '****' stuff is nothing, i replaced my company name with it.
Of course you may put the context processor anywhere else, that is your choice.
Good luck!
Alexander
Add this to your model to be able to get their admin change link from anywhere
Useful if you want to jump to the admin screen of an object you are looking at on the front end
This will use the syslog system to log your logs. This is usefull when you use WSGI and want to have a per day logging file (TimedRotatingFileHandler is not process safe, and you may lose data when using it with WSGI).
Under a debian system, you'll have to modify **/etc/rsyslog.conf** and add:
local0.* -/var/log/django/django.log
local1.* -/var/log/django/payment.log
When called, this module dynamically alters the behaviour of model.save() on a list of models so that the SQL is returned and aggregated for a bulk commit later on. This is much faster than performing bulk writing operations using the standard model.save().
To use, simply save the code as django_bulk_save.py and replace this idiom:
for m in model_list:
# modify m ...
m.save() # ouch
with this one:
from django_bulk_save import DeferredBucket
deferred = DeferredBucket()
for m in model_list:
# modify m ...
deferred.append(m)
deferred.bulk_save()
Notes:
* - After performing a bulk_save(), the id's of the models do not get automatically updated, so code that depends on models having a pk (e.g. m2m assignments) will need to reload the objects via a queryset.
* - post-save signal is not sent. see above.
* - This code has not been thoroughly tested, and is not guaranteed (or recommended) for production use.
* - It may stop working in newer django versions, or whenever django's model.save() related code gets updated in the future.