Automatically activates the virtualenv when running manage.py command.
Just create requirements.pip file inside the root of django project and run ./manage.py update_ve in first time.
Code checks requirements.pip and virtual env timestamps and require to recreate outdated environment.
This decorator function wraps a normal view function
so that it can be read through a jsonp callback.
Usage:
@AllowJSONPCallback
def my_view_function(request):
return HttpResponse('this should be viewable through jsonp')
It looks for a GET parameter called "callback", and if one exists,
wraps the payload in a javascript function named per the value of callback.
Using AllowJSONPCallback implies that the user must be logged in
(and applies automatically the login_required decorator).
If callback is passed and the user is logged out, "notLoggedIn" is
returned instead of a normal redirect, which would be hard to interpret
through jsonp.
If the input does not appear to be json, wrap the input in quotes
so as not to throw a javascript error upon receipt of the response.
You've filtered your changelist in your admin site and you want to edit a few entries here; you click on an object, edit it and press save, and you end up back at the default unfiltered changelist view. This ModelAdmin override is so that if you press "save" (not "save and add another", or "save and continue editing") you end up back at your filtered changelist.
There are other ways out there and other snippets to do similar; however I hadn't seen one to only redirect if you pressed save so this is what I came up with. Hopefully it's useful.
The syndication documentation gives a very basic beginning to creating an iTunes Podcast Feed, but leaves a lot of work left to be figured out. This is a completed example. Because I needed to obtain the duration of the podcast file programmatically (not part of this code) I limit the podcast to mp3 files. If you want to obtain durations some other way you can set the mime_type in the same manner as the other properties.
This is what I'm using and should be plenty for you to customize from.
This middleware will prevent access to the admin if the users IP isn't in the INTERNAL_IPS setting, by comparing the request path with the reversed index URL of the default admin site, ultimately raising a 404 (unless DEBUG = True).
I needed to use class based views, but I wanted to be able to use the full name of the class in my URLconf without always having to instantiate the view class before using it. What helped me was a surprisingly simple metaclass.
I can now both instantiate view classes and use the instances as view functions, OR I can simply point my URLconf to my class and have the metaclass instantiate (and call) the view class for me. This works by checking the first argument to `__call__` – if it's a `HttpRequest`, it must be an actual HTTP request because it would be nonsense to attept to instantiate a view class with an `HttpRequest` instance.
The `View` base class contains a overridable before method that can be used to add a common procedure to handlers of different HTTP requests. The `before` method can modify the args and kwargs of the request handlers to be able to replace, say, `model_id` with `model`.
*WARNING* This is *extremely* slow.
This snippet allows you to easily prevent *most* race conditions (if used properly).
Feel free to extend on top of this as you like, I'd appreciate any comments to [email protected]
### Problem: you want to limit posts to a view
This can be accomplished with a view decorator that stores hits by IP in memcached, incrementing the cached value and returning 403's when the cached value exceeds a certain threshold for a given IP.
A simple django-admin filter to allow a boolean-like filter for IS NULL/IS NOT NULL.
By default it applies to CharField, IntegerField, and FileField, but you can change this by editing NullFilterSpec.fields.
A replacement for sending mail that takes the name of a template which exists in both text and html formats, and uses these to create a multipart email populated with the given context.
This combination of settings.py admin_reorder_tag.py and admin/base_site.html gives you the ability to define custom ordering for the apps and models in the admin app.
1. Add the setting ADMIN_REORDER to your settings.py as a tuple with each item containing a tuple of the app name and a tuple of the models within it, all defining the ordering to apply.
2. Drop the template tag code into admin_reorder_tag.py and put this into a templatetag package in one of your installed apps.
3. Drop the template code into your templates directory as admin/base_site.html
I often find myself needing to create a template tag and all I'm interested in is the token and the render function. This decorator abstracts away the boilerplate code around creating the template node class which is the same each and every time.
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>
...
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.