Login

All snippets written in Python

Snippet List

Generic admin action export selected rows to excel

Based on [Snippet 2558](http://djangosnippets.org/snippets/2558/) but without saving the generated file to disk before downloading. Requires, pyExcelerator Usage: Add the code to your project, e.g. a file called actions.py in the project root. Register the action in your apps admin.py: `from myproject.actions import export_as_xls class MyAdmin(admin.ModelAdmin): actions = [export_as_xls]`

  • admin
  • export
  • admin-actions
  • xls
Read More

HTTPS redirections middleware with updated URL template tag

This middleware redirects HTTP requests to HTTPS for some specified URLs, in the same way as [85](http://djangosnippets.org/snippets/85/). It also changes the `url` template tag to use the `https` scheme for the same URLs. For example, if you have the following URL pattern: url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'https': True}) then the template: {% from future import url %} {% url 'django.contrib.auth.views.login' %} will render: https://host.example.com/accounts/login/ and any plain HTTP requests to /accounts/login get redirected to HTTPS. URL patterns not marked with `'https': True` remain unaffected. Notes: * The HttpRequest object must be present in the template context as `request`, so add `django.core.context_processors.request` to `TEMPLATE_CONTEXT_PROCESSORS` and make sure to use `RequestContext`. * This snippet overrides the existing `url` template tag. Remove the last line and register the new `url` function properly, as a separate tag, if this makes you unhappy. You'd then have to change your templates to use it. * It would be nicer to change the way reverse look-ups behave instead of changing only the `url` template tag, but the URL resolver, and the `reverse` function, know nothing about requests, so have no way to find the correct host name.

  • middleware
  • template
  • url
  • ssl
  • reverse
  • https
  • redirection
  • tls
Read More
Author: xlq
  • 0
  • 2

Tastypie MongoDB Resource

MongoDB Resource for Tastypie ============================= Allows you to create delicious APIs for MongoDB. Settings -------- MONGODB_HOST = None MONGODB_PORT = None MONGODB_DATABASE = "database_name" Example of Usage ---------------- from tastypie import fields from tastypie.authorization import Authorization from tastypie_mongodb.resources import MongoDBResource, Document class DocumentResource(MongoDBResource): id = fields.CharField(attribute="_id") title = fields.CharField(attribute="title", null=True) entities = fields.ListField(attribute="entities", null=True) class Meta: resource_name = "documents" list_allowed_methods = ["delete", "get", "post"] authorization = Authorization() object_class = Document collection = "documents" # collection name Github Repository ================= <https://github.com/fatiherikli/tastypie-mongodb-resource>

  • tastypie
  • mongodb
Read More

Amazon S3 browser-based upload form(FIXED)

This is a 'fixed' version of snippet [1868](http://djangosnippets.org/snippets/1868/) Changes: *Correctly handle the Content-Type, because amazon requieres it to be named with a dash and we can't use dashes in the form attributes declaration. *Also added max_size handling, with the corresponding update to the policy generation. *Added an example usage with some javascript for basic validation. [See the amazon reference](http://aws.amazon.com/articles/1434?_encoding=UTF8&jiveRedirect=1)

  • s3
  • amazon
  • html form
  • upload form
Read More

boostrap append-input for widgets

Append span with text, image or other data to any django widget so bootstrap can format it like in [here](http://twitter.github.com/bootstrap/base-css.html#forms) (scroll to "Extending form controls" section) Example usage: ` example_field = CharField( max_length=255, min_length=1, label='Label', required=False, widget=AppendWidget(base_widget=TextInput, data='@') ) `

  • django
  • bootstrap
  • append-input
Read More

boostrap prepend-input for widgets

Prepend span with text, image or other data to any django widget so bootstrap can format it like in [here](http://twitter.github.com/bootstrap/base-css.html#forms) (scroll to "Extending form controls" section) Example usage: ` example_field = CharField( max_length=255, min_length=1, label='Label', required=False, widget=PrependWidget(base_widget=TextInput, data='@') ) `

  • django
  • bootstrap
  • prepend-input
Read More

Active page class for selected menu items

Simple tag to check which page we are on, based on resolve: useful to add an 'active' css class in menu items that needs to be aware when they are selected. Typical usage is like: ` <ul> <li class="{% active request "myapp:myview1" %}">My View 1</li> <li class="{% active request "myapp:myview2" %}">My View 2</li> </ul> `

  • templatetag
  • menu
  • tab
Read More

List all errors in a form +bootstrap highlighting

Use this snippet to list all errors in a form. The message will be shown in a boostrap-type alert which can be 'closed' using a dismiss button. The **field label** and the **error** will be listed. e.g. > * Name: This field is required > * Email: Please enter a valid email

  • template
  • forms
  • error
  • form
  • list
  • errors
Read More

Filter to add zero-width space to break up long words

If you have long words (no spaces) that are so long that it's messing up your design, add a 0-width space in the word every X chars. Usage: Step 1. Inside your app's directory, create dir called 'templatetags'. In that directory, create a .py file (say 'app_extras.py'). Make sure you make this a python module, make an empty the init .py (with the 2 underscores on each side). Step 2. Inside template (make sure app is on INSTALLED_APPS list in settings.py): {% load app_extras %} Step 3. Enjoy! {{ some_long_word_with_no_breaks|zerowidthspace_separator:25 }}

  • wordwrap
  • wordbreak
  • word-break
  • long-words
  • break
  • long-lines
  • wrap
  • char-break
Read More

Pre-delete signal function for deleting files a model

This snippit is meant to be used with the pre_delete signal to delete any files associated with a model instance before the instance is deleted. It will search the model instance for fields that are subclasses of FieldFile, and then delete the corresponding files. As such, it will work with any model field that is a subclass of FileField.

  • remove
  • delete
  • signals
  • file
Read More

2955 snippets posted so far.