Login

Tag "path"

Snippet List

Unique naming for file uploads

This snippit can be used to generate unique file names for uploads. `upload_to` allows a callable, but only provides two arguments: `instance` and `filename`. In order to prevent dumping all of the files for a model into one directory, this utilizes the `partial` decorator from `functools`, which essentially allows adding an extra argument to the function. The returned path will be of the form: `[model name]/[field_name]/[random hash].[filename extension]`

  • files
  • path
  • naming
  • uploads
Read More

Active link

I needed a way to find if a menu items should be active. After searching the internet i found a few options*, but none of them did fit my needs, so i wrote my own: Usage: <a href="{% url 'view-name' %}" class="{% current request 'view-name' %}"></a> * http://gnuvince.wordpress.com/2008/03/19/the-new-and-improved-active-tag/ * http://stackoverflow.com/questions/340888/navigation-in-django

  • template
  • path
  • active
  • link
  • current
Read More

location context_processor

A simple context_processor to include location info. Useful for permalinks, site name references, and navigation bars. For example: {% if location.path|match:"/$" %} class="current"{% endif %} See also my [match filter](/snippets/1686/).

  • url
  • path
  • location
  • nav
Read More

Get the referer view of a request

Get the referer view of a request. **Example:** def some_view(request): ... referer_view = get_referer_view(request) return HttpResponseRedirect(referer_view, '/accounts/login/')

  • view
  • referer
  • request
  • path
Read More

Add URL Segments to Templates

Add this code to you your context_processors.py in your project and then install it in your settings.py TEMPLATE_CONTEXT_PROCESSORS. In your template you can print out a segment of a url by using {{ segment_1 }}. For example if you're on the page "/mysite/section1/section2/" and you used {{ segment_2 }} it would print section1. This idea was taken from Expression Engines URL Segments, http://expressionengine.com/docs/templates/globals/url_segments.html. This comes in handy if you only want to do something in your template if the page your on has a particular segment. FYI, I haven't used this in a production setting yet so it could be buggy still.

  • template
  • url
  • path
  • segment
Read More

djangopath: conveniently set sys.path and DJANGO_SETTINGS_MODULE

In the past, whenever I had a script that I wanted to properly configure the settings for, I would use something like the following idiom at the top of the script: import sys, os; dirname = os.path.dirname # sys.path.insert(0, dirname(dirname(__file__))) sys.path.insert(0, dirname(__file__)) os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings' Notice that this is a relative setting to `__file__` variable in the script. The djangopath function is an attempt to do away with the above such that I can now write the following: from lib import djangopath; djangopath(up=2, settings='myapp.settings') This seems to work for me, but it assumes that you are packaging your script inside your projects/apps. If they are elsewhere then you may need to resort to another method (e.g. absolute paths, etc.) AK

  • settings
  • path
Read More

chdjango

This is a convenient script for those working with different branches of Django. Place all of your branches in `~/django` (e.g., `~/django/newforms-admin`, `~/django/trunk`) and you're ready to quickly change between them. For example: `chdjango.py trunk`.

  • django
  • path
  • change
  • chdjango
  • branches
  • pth
  • site-packages
Read More

ImageField with per user folder

If you need to upload Image files into folders qualified with user name eg.: 'images/user1/2008/01/01' then you may use this snippet. In order to use it you have to install ThreadLocals middleware as described here: http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser Then just import UserImageField class at your models.py and specify 'upload_to' parameter with '%(user)s' in the path eg.: ` image3 = UserImageField(_('Image 3'), upload_to='flower_images/%(user)s/%Y/%m/%d', null=True, blank=True) `

  • imagefield
  • path
Read More

manage.py with magic python path

I tend to have all my python code together in a 'python' directory. e.g. a typical project layout of mine looks like: /python /python/myproject - project python code /python/django - local copy of django /python/foolib - some third party library /media /templates ... Since I don't want to set the python path explicitly I just assume the 'manage.py' is in the right place and use its __file__ variable to set up the python path correctly. I use the same trick for my settings.py for setting MEDIA_ROOT and TEMPLATE_DIRS.

  • python
  • path
  • magic
  • pythonpath
  • sys.path
Read More

Single Django App behind multiple Apache Proxies

** Django Proxied Behind Apache Path** Middleware and Context Processor for django applications behind a proxy like Apache's mod_proxy. Setting an HTTP header with the base path of the django application in the Apache mod_proxy configuration, we can pull that variable out of the request and adjust our template URIs and redirects. This allows us to serve the same Django application out from behind multiple VirtualHosts at different URL paths. Example use in templates: <a href="{{ base_path }}/login/">Login</a> Example Apache configuration: <LocationMatch ^/path/to/django/app/.*> RequestHeader set X-Base-Path "/path/to/django/app" </LocationMatch> RewriteRule ^/path/to/django/app/(.*) http://django-host:8080/$1 [P,L] In settings.py: VHOST_HEADER = "HTTP_X_BASE_PATH"

  • apache
  • virtualhost
  • mod_proxy
  • path
Read More

13 snippets posted so far.