Login

Snippets by masida

Snippet List

Export queryset to Excel workbook

How to use =========== Save the snippet to a file utils.py, and add the following view to your Django app: from django.http import HttpResponse from .utils import queryset_to_workbook def download_workbook(request): queryset = User.objects.all() columns = ( 'first_name', 'last_name', 'email', 'is_staff', 'groups') workbook = queryset_to_workbook(queryset, columns) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="export.xls"' workbook.save(response) return response Note: you can use dotted notation (`'foreign_key.foreign_key.field'`) in the columns parameter to access fields that are accessible through the objects returned by the queryset (in that case you probably want to use `select_related` with your queryset).

  • export
  • queryset
  • xlwt
Read More

Display values from a bound (submitted) form

Function that takes a bound form (submitted form) and returns a list of pairs of field label and user chosen value. It takes care of: 1. fields that are not filled out 2. if you want to exclude some fields from the final list 3. ChoiceField (select or radio button) 4. MultipleChoiceField (multi-select or checkboxes) Usage: if form.is_valid(): form_data = get_form_display_data(form, exclude=['captcha']) It's trivial to display the list of pairs in a template: {% for key, value in form_data %} {{ key }}: {{ value }}{% endfor %}

  • template
  • email
  • form
Read More

Calendar template-tag

Simple template tag to show a calendar. I use it to display events (which is a model with a start_date and end_date attribute. You probably should change this according to your needs.

  • template
  • tag
  • calendar
Read More

masida has posted 3 snippets.