Login

Tag "django"

Snippet List

Model Choices Helper

This is my attempt at a convenience class for Django model choices which will use a [Small]IntegerField for storage. It's very similar to [jacobian's version](http://www.djangosnippets.org/snippets/1664/), but I wanted to be able to use simple attributes for access to the integer values. It's not technically dependent on Django, but it's probably not a datatype that would be useful for much else. Feel free to do so however if you have a use-case. >>> statuses = Choices( ... ('live', 'Live'), ... ('draft', 'Draft'), ... ('hidden', 'Not Live'), ... ) >>> statuses.live 0 >>> statuses.hidden 2 >>> statuses.get_choices() ((0, 'Live'), (1, 'Draft'), (2, 'Not Live')) This is then useful for use in a model field with the choices attribute. >>> from django.db import models >>> class Entry(models.Model): ... STATUSES = Choices( ... ('live', 'Live'), ... ('draft', 'Draft'), ... ('hidden', 'Not Live'), ... ) ... status = models.SmallIntegerField(choices=STATUSES.get_choices(), ... default=STATUSES.live) It's also useful later when you need to filter by your choices. >>> live_entries = Entry.objects.filter(status=Entries.STATUSES.live)

  • django
  • models
  • choices
Read More

Fieldsets for Views

This Snippet allows a view to controle the printed forms on the templates, in a similar way to the fieldsets used by the django admin. How to Use: In the view in question, put: def some_view(request): ... fieldsets = ( (u'Title 1', {'hidden' : ('field_1', 'field_2',), 'fields' : ('field_3',)}), (u'Title 2', {'hidden' : ('field_5', 'field_6',), 'fields' : ('field_4',)}),) ) return render_to_response('some.html', {'fieldsets': fieldsets}) fieldsets = ( (None, {'hidden' : ('evento', 'colaborador',), 'fields' : ('acompanhantes',)}), ) Next, in the html just add: <form enctype="multipart/form-data" id="edit" method="post" ...> ... {% include "inc/form_snippet.html" %} ... <input type="submit" value="Submit"> </form>

  • template
  • django
  • admin
  • views
  • filters
  • python
  • tags
  • html
  • css
  • dicts
Read More
Author: Nad
  • 2
  • 0

Admin Apps Names Translation

This Snippet allows for your project's apps names to be displayed as you want in the Admin, including translations. The lists of apps and languages are created from your settings.py file. **How to use** 1st part: - Create a application called 'apps_verbose' in you project with the models.py and admin.py showed here - Create a folder inside it with named 'templatetags' with the verbose_tags.py file inside it. - Add this app to installed apps in your settings.py - If you change this app name, dont forget to correct the imports. 2nd part: - Create a folder named 'admin' in your templates folder and copy the following files form your /django/contrib/admin/templates/admin/ folder. - /app_index.html - /base.html - /change_form.html - /change_list.html - /delete_confirmation.html - /delete_selected_confirmation.html - /index.html - /object_history.html - Make the necessary changes in each file, like shown here. 3rd part: - Create translations in the Admin and enjoy.

  • template
  • django
  • admin
  • i18n
  • python
  • tags
  • html
  • app
  • translation
Read More
Author: Nad
  • 3
  • 5

Auto-create Django admin user during syncdb

This avoids the frustrating step of having to set up a new admin user every time you re-initialize your database. Put this in any `models` module. In this example I use `common.models`. Adapted from http://stackoverflow.com/questions/1466827/

  • django
  • admin
  • user
  • syncdb
  • manage
  • automatic
  • adminuser
  • admin-user
Read More

All Imports Checker

I was using flup to run django in fcgi mode and encountered the dreaded "Unhandled Exception" page quite frequently. So apart from all the precautions about handling this except, I wrote the above code snippet, which checks the import across your ENTIRE project. Ofcourse this can be used on any python project, but I have written it for my favorite framework django. It is now written as a Django command extension, an can be run as: **python manage.py imports_checker** This is a generic command, it does not check the settings.INSTALLED_APPS setting for cleaning. But can be improved to do the same. Public Clone Url: [git://gist.github.com/242451.git](git://gist.github.com/242451.git) Update: Now it supports checking imports, just only at the app level also usage: python manage.py imports_checker &lt;appname&gt;

  • django
  • python
  • imports
Read More

django paginator

This a basic pagination example. It shows new 5 pnews items. We added a code to our template so we can view previous or next pages. It also show us how many pages we have.

  • django
  • paginator
Read More

counter templatetag

The counter initializes the variable to 0, and next it increments one by one: {% load counter_tag %} {% for pet in pets %} {% if pet.is_cat %} {% counter cats %} {% else %} {% counter dogs %} {% endif %} {% endfor %} # cats: {{cats}} # dogs: {{dogs}}

  • template
  • django
  • counter
  • increment
  • loop
Read More

pyText2Pdf - Python script to convert plain text into PDF file. Modified to work with streams.

This is "pyText2Pdf" - Python script to convert plain text into PDF file. Originally written by Anand B Pillai. It is taken from http://code.activestate.com/recipes/189858/ Modified to work with streams. Example: produce PDF document from text and output it as HTTPResponse object. import StringIO input_stream = StringIO.StringIO(text) result = StringIO.StringIO() pdfclass = pyText2Pdf(input_stream, result, "PDF title") pdfclass.Convert() response = HttpResponse(result.getvalue(), mimetype="application/pdf") response['Content-Disposition'] = 'attachment; filename=pdf_report.pdf' return response

  • django
  • tools
  • python
  • pdf
  • converter
  • text2pdf
  • adobe
  • acrobat
  • processing
Read More

Rails-like environments using Django

This is a replacement for settings.py, which moves the actual settings files into a directory called "env" and establishes different versions for different settings of the environment variable DJANGO_ENV. At runtime, the specified development environment can be found and loaded into the local context of settings.py, which is then picked up by whatever routine manage.py is kicking off.

  • django
  • rails
  • environment
Read More

Interactive debugger and other Paste niceties

runserver with extra development tools: 1. interactive debugger that pops up automatically if an exception occurs, courtesy of [Werkzeug](http://werkzeug.pocoo.org/documentation/dev/debug.html#using-the-debugger) 2. logging of print statements directly into HTML (div float), courtesy of [Paste](http://pythonpaste.org/modules/debug.prints.html). Credits: [piranha.org.ua](http://piranha.org.ua/)

  • print
  • django
  • debugging
  • paste
  • wsgi
  • werkzeug
Read More

Printing inline formsets as UL / P

By default all forms created using inlineformset_factory are displayed as tables (because there is only a .as_table method) and there are no .as_p or .as_ul methods in them, so you need to do that by hand.

  • django
  • formset
  • inline
  • not-django-admin
Read More

213 snippets posted so far.