Tumblelog by Soup.io
Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.

June 29 2012

December 04 2009

accessing request and media_url in templates

-> settings.py
    'django.core.context_processors.request', # <-- don't forget to insert the default processors when overriding
    "django.core.context_processors.media" # <-- default

-> views.py
from django.template import RequestContext
from django.shortcuts import render_to_response

def foobar(request):
    return render_to_response( 'template.html', dictionary = your_dic, context_instance = RequestContext( request ) )

-> template.html
<img src="{{ MEDIA_URL }}/img/banner.png"/>
<li {% ifequal request.path '/'  %}class="active"{% endifequal %}>
        <a href="/">Home</a>

October 08 2009


Accessing current user inside a model.

October 06 2009

File Download

from django.http import HttpResponse
from django.core.servers.basehttp import FileWrapper
import os

def file_download( filename ):
    wrapper = FileWrapper( file( filename ) )
    response = HttpResponse( wrapper, content_type = guess_type( filename )[0] )
    response['Content-Length'] = os.path.getsize( filename )

    return response

October 01 2009

Django request variables

Get all request variables from your http request

September 25 2009

September 18 2009

Personal plural in admin interface

class Category(models.Model):
class Meta:
verbose_name = "Category" # Defines own singular name
verbose_name_plural = "Categories"

With translation (Djangobook):

from django.utils.translation import ugettext_lazy as _

class Category(models.Model):
class Meta:
verbose_name = _("Category") # Defines own singular name
verbose_name_plural = _("Categories")

September 13 2009

Old session problem between mod_python and php5

Maybe this is the problem at my etch server.
Edit: I tested this and after deactivating the mhash admin-login works, but the PHP apps on the server need mhash :-/

August 29 2009

Access to ForeignKey/M2M fields

You can access a field from an ForeignKey/M2M with double underscore, like this:

yourmodel.objects.all().values( 'foo__bar' )
- get all field content 'bar' from ForeignKey 'foo' as dictionary, you can also use 'foo__bar' with filter etc.

August 27 2009

Iterating over a dictionary in Django templates

Short answer:
{% for key,value in dictionary.items %}{{ value }}{% endfor %}
Older posts are this way If this message doesn't go away, click anywhere on the page to continue loading posts.
Could not load more posts
Maybe Soup is currently being updated? I'll try again automatically in a few seconds...
Just a second, loading more posts...
You've reached the end.

Don't be the product, buy the product!