To put obfuscated primary keys in any class, simply inherit from this one. For example:
class Offer(ObfuscatedPKModel)
You can match for these bigint primary keys in your urls.py like this:
'^offer/(?P<offer_pk>[0-9\-]+)$'
Assuming you have defined STATIC_ROOT correctly in settings.py, and have installed staticfiles_urlpatterns() in urls.py (as demoed in the code, taken from Django's staticfiles docs), the code from static_root_finder.py can be used to simply serve static files in development from STATIC_ROOT directly, avoiding complicated setups in case we just want to have a bunch of static files in one directory.
In templates sometimes you need to display some menu by checking whether the user is logged in or not. So use the above filter as shown below
{% with request|check_login as logout %}
{% if logout%}
display something....
{% endif %}
{% endwith %}
This makes a 100x100 thumbnail of the image for the image field and shows it in the admin form
combination of http://djangosnippets.org/snippets/955/ and http://www.psychicorigami.com/2009/06/20/django-simple-admin-imagefield-thumbnail/
The above conf file the easiest way to point the env and the settings together , the user has to point the django.wsgi file with the correct path and it should work fine and one can directly run it in apache
Utility functions for generating dummy ("lorem ipsum") text, with text from
the "Karel ende Elegast" old-Dutch epic poem. [wikipedia](http://en.wikipedia.org/wiki/Karel_ende_Elegast)
by Wicher Minnaard <[email protected]> [website](http://smorgasbord.gavagai.nl/)
Sort-of monkey-patches django.contrib.webdesign.lorem_ipsum (it's an experiment).
Hopefully threadsafe, so both lorem_ipsum and elegast module functions can be
accessed simultaneously.
The snippet enables decoupling model classes, associated with a ForeignKey, for the purpose of separating them into two databases. Looking at the following example:
class Reporter(models.Model):
...
class Article(models.Model):
reporter = models.ForeignKey(Reporter)
We want to separate the `Reporter` and `Article` into two separate databases, but this won't work in Django because of the [cross model relations](http://docs.djangoproject.com/en/dev/topics/db/multi-db/#cross-database-relations). The solution is to use an ID field and not a `ForeignKey` one. This makes the access very uncomfortable. The above class will make this a bit less awkward. It doesn't support the [RelatedManager](http://docs.djangoproject.com/en/dev/ref/models/relations/#django.db.models.fields.related.RelatedManager), but it will support accessing the related field as you normally would.
The article class will look like this (assuming the reporter model id field is an `IntegerField`):
class Article(DecoupledModel):
reporter_id = models.IntegerField()
_linked_fields = {
'reporter': Reporter,
}
Once you have an article object, you can access the reporter as you normally would for both read and writing. For example:
my_reporter = article1.reporter
article2.reporter = my_reporter
This widget will produce a select box with the range of dates that you input.
**Usage:**
`widget=SelectDateWidget('2010-12-15', '2010-12-20')`
**Output:**
`<select>
<option value="2010-12-15">Wed January 01, 2010</option>
<option value="2010-12-16">Thu January 02, 2010</option>
<option value="2010-12-17">Fri January 03, 2010</option>
<option value="2010-12-18">Sat January 04, 2010</option>
<option value="2010-12-19">Sun January 05, 2010</option>
<option value="2010-12-20">Mon January 06, 2010</option>
</select>`
Shows difference between two json like python objects. May help to test json response, piston API powered sites...
Shows properties, values from first object that are not in the second.
Example:
import simplejson # or other json serializer
first = simplejson.loads('{"first_name": "Poligraph", "last_name": "Sharikov",}')
second = simplejson.loads('{"first_name": "Poligraphovich", "pet_name": "Sharik"}')
df = Diff(first, second)
df.difference is ["path: last_name"]
Diff(first, second, vice_versa=True) gives you difference from both objects in the one result.
df.difference is ["path: last_name", "path: pet_name"]
Diff(first, second, with_values=True) gives you difference of the values strings.
Very simple python class for querying Google Geocoder. Accepts a human-readable address, parses JSON results and sets lat and lng. (Full JSON results are stored to `results` property of GoogleLatLng for access to other attributes.)
This could easily be expanded to include the XML output option, etc.
**Requires PycURL and simplejson.**
Example:
>>> location = "1600 Amphitheatre Parkway, Mountain View, CA 94043"
>>> glatlng = GoogleLatLng()
>>> glatlng.requestLatLngJSON(location)
>>> print "Latitude: %s, Longitude: %s" % (glatlng.lat, glatlng.lng)
Latitude: 37.422782, Longitude: -122.085099`
** Do not forget the usage limits, which include request rate throttling, otherwise, Google might ban you. **
===
Mechanism to obtain a `request.user` object without the `request` object itself. Requires `LocalUserMiddleware` in `MIDDLEWARE_CLASSES` settings variable.
**Important**: works under assumption that within a web server each request is handled by a separate thread (as for example in the Apache HTTP server).
**Beware**: [security threat](http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser), although ["thread locals only appears to be a security threat if a system has already been seriously compromised, at which point there'd be easier attacks to execute"](http://groups.google.com/group/django-users/browse_thread/thread/e7af359d7d183e04).
**Dev note**: works fine with one-threaded Django's development server, each request resets current user; no worries 'bout many media requests - they won't (at least shouldn't) be using Django on the production server.
**Ref**: originally found in the gatekeeper app.
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.