Skip to content

Django tips

Michael Hulse edited this page Jul 17, 2019 · 2 revisions

Last modified: Jan 25, 2015
Updated to fix formatting on July 17, 2019

How to delete template cache fragment

Navigate to your Django project.

In this example, I want to delete a cache key named "footer".

Fire up the Django python interpreter:

$ python manage.py shell

At the python prompt, run:

>>> import hashlib
>>> from django.core.cache import cache
>>> from django.template import resolve_variable
>>> from django.utils.http import urlquote
>>> args = hashlib.md5(u':'.join([urlquote('')]))
>>> cache_key = 'template.cache.%s.%s' % ('footer', args.hexdigest())
>>> cache.has_key(cache_key)
True
>>> cache.delete(cache_key)
>>>

Done!

How to delete template cache fragment

Kill the template fragment cache:

'''
See https://code.djangoproject.com/browser/django/trunk/django/templatetags/cache.py#L25
First, get the cache key … 
'''

>>> import hashlib
>>> from django.core.cache import cache
>>> from django.template import resolve_variable
>>> from django.utils.http import urlquote

'''
The hashlib argument below can be empty, i.e. ''
'''

>>> args = hashlib.md5(u':'.join([urlquote('RG Index: UO President Richard Lariviere')]))
>>> cache_key = 'template.cache.%s.%s' % ('generic_index_cache', args.hexdigest())
>>> cache.has_key(cache_key)
True
>>> cache.delete(cache_key)

Kill a page URL cache

>>> from django.core.cache import cache
>>> from django.http import HttpRequest
>>> from django.utils.cache import _generate_cache_header_key
>>> request = HttpRequest()
>>> key_prefix = ''
>>> request.path = '/topics/lariviere/'
>>> key = _generate_cache_header_key(key_prefix, request)
>>> key
'views.decorators.cache.cache_header..0fed4bf32cb4982c2b50402ea06ca664.en-us'
>>> cache.has_key(key)
True
>>> cache.set(key, None,  0)
>>> cache.has_key(key)
False

How to update a Django model

  1. Prepare your fixture:
python manage.py dumpdata dox --indent=2 --format=json > ./dox/fixtures/dox.20140520.json
  1. Using same code above, backup the fixture that you'll be replacing.

  2. Open two terminal windows (we'll call them windows A and B respectively) and log in to same server.

  3. In both windows, cd to Django project.

  4. In window A, type: python manage.py sqlclear dox (where dox is the app name). Note that sqlreset would also work, but that gives us more info than is needed.

  5. In window B, type: python manage.py dbshell; more info on dbshell here.

  6. From window A, copy each line's of SQL into window B's database shell prompt (=>); for example:

=> BEGIN;
=> DROP TABLE "dox_file" cascade;
=> ALTER TABLE "dox_page" DROP CONSTRAINT "status_id_refs_id_e05550a5" cascade;
=> DROP TABLE "dox_pagestatus" cascade;
=> DROP TABLE "dox_page" cascade;
=> COMMIT;

Note: We've found that it is best to add cascade at the end of each line/statement.

Once you commit, the app's tables have been officially dropped (you should get positive feedback from the terminal after each line's execution).

  1. Next, in window A, run python manage.py syncdb to initialize your apps tables (i.e., the ones you just dropped above).

  2. Finally, in window A, load your fixture: python manage.py loaddata ./dox/fixtures/dox.20140520.json

  3. In window A, touch the wsgi touch apache/django.wsgi to reload the Django instance. Or, you could restart Apache: sudo service apache2 restart (Unbuntu).

That's it! You should have fresh data. Check you admin. If something went wrong, load the backup data and/or fix the fixture you were trying to import.

If all goes well, you can exit from both windows.

Clone this wiki locally