Dynamic Keyword Arguments (**kwargs)

http://stackoverflow.com/questions/337688/dynamic-keyword-arguments-in-python

Probably rarely needed but may come in handy.

arg_name = 'myarg'
some_method(**{arg_name: 'value'})

view raw
basics.py
hosted with ❤ by GitHub

Cleanup PYC Files

These should be automatically regenerated on the fly but I’ve noticed that if you restructure your code (i.e. move things around) they can get out of whack. Best to clean them up from time to time…

find . -name '*.pyc' -exec rm {} \;

view raw
clean.sh
hosted with ❤ by GitHub

Static Class or Simple Functions

Having worked in a variety of other languages, my first instinct was to create classes with static methods for things like utility libraries. But after digging around Google for a bit, it seems like this isn’t necessarily the way things are done in Python:

http://stackoverflow.com/questions/12735392/python-class-static-methods

I’m not sure where I stand on this just yet. The logical argument is that classes should only be used for when you need to instantiate something. But sometimes it’s handy to make use of class variables. And really the only difference is a small change in syntax:

import filename
filename.foo()
from filename import ClassName
ClassName.foo()

view raw
basics.py
hosted with ❤ by GitHub

Django Timezone Aware Dates

UPDATE: Looks like this shortcut method does the same thing (as long as USE_TZ = True in your settings):

from django.utils import timezone
timezone.now()

view raw
basics.py
hosted with ❤ by GitHub

The following will create a timezone-aware datetime object:

import datetime
from django.utils.timezone import utc
now = datetime.datetime.utcnow().replace(tzinfo=utc)

view raw
basics.py
hosted with ❤ by GitHub

Read more here: https://docs.djangoproject.com/en/dev/topics/i18n/timezones/

Deleting a Django Session Variable

http://stackoverflow.com/questions/2375335/in-django-how-do-i-clear-a-sessionkey

Goes for sessions and just about any other type of variable:

del request.session['key']

view raw
basics.py
hosted with ❤ by GitHub

Django Hidden Fields

Quick shortcut for including hidden fields in your forms:

forms.py

class SomeForm(forms.Form):
hidden = forms.CharField(required=False, max_length=50, widget=forms.HiddenInput())

view raw
forms.py
hosted with ❤ by GitHub

template.html

{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}

view raw
template.html
hosted with ❤ by GitHub

Now you don’t have to worry about each and every hidden field.

Checking if a GET Variable Exists in Django

http://stackoverflow.com/questions/2422055/how-to-check-if-request-get-var-is-none

First, check if the request.GET dict contains a parameter named q:

if request.method == 'GET' and 'q' in request.GET:

view raw
basics.py
hosted with ❤ by GitHub

Now ensure the variable has a value:

q = request.GET['q']
if q:
# Do processing here

view raw
basics.py
hosted with ❤ by GitHub

Multiple Files for Django Views

Separating your views into multiple files works much the same way as models except for one difference – importing the views in __init__.py isn’t necessary unless you want to type a little less.

Without importing them in __init__, you’ll need to do this:

from app.views.blog import Blog

view raw
import.py
hosted with ❤ by GitHub

But if you add them, you can do the following instead:

# import one view
from app.views import Blog
# import a bunch
from app.views import *

view raw
import.py
hosted with ❤ by GitHub