Tag Archive | "Django"

Using a favicon in Django

Sunday, November 1, 2009

Comments Off on Using a favicon in Django

The default location for a favicon.ico file is the root folder of website. However when using Django with an apache virtualhost, you need to map the location of your favicon. Here are a few ways to get you favicon working: 1) The quick way is to modify the html in your base template to include […]

Continue reading...

Dynamically Importing and Instantiating Modules in Python

Tuesday, October 13, 2009

Comments Off on Dynamically Importing and Instantiating Modules in Python

Also called reflection, here is one method to import modules and instantiate objects using strings: moduleName = 'myApp.models' className = 'Blog' #import the module by saying 'from myApp.models import Blog' module = __import__(moduleName, {}, {}, className) #now you can instantiate the class obj = getattr(module, className )() #set a property of the object using a […]

Continue reading...

Resolving DJANGO Error: “ValueError: invalid literal for int() with base 10: ‘None'”

Sunday, October 11, 2009

Comments Off on Resolving DJANGO Error: “ValueError: invalid literal for int() with base 10: ‘None'”

If you receive this error via email while viewing one of your admin pages, here are some steps to help resolve it. First note the problem URL. It should be in the detailed error email. Try the url in your browser and verify you receive the same error. Now you have to find the page […]

Continue reading...

Getting Distinct Rows Using Django Database Layer

Thursday, October 1, 2009

2 Comments

To get the equivalent of the SQL select distinct statement in Django, you can use a combination of the values() and distinct() method of the QuerySet api. For example, you have a musicians table and you want to get list of instruments that they play (with no duplicates): Musicians.objects.values('instrument').distinct() The values(‘instrument’) method will generate a […]

Continue reading...

How to Get a Client IP Address in DJANGO

Friday, August 14, 2009

5 Comments

Here is a small snippet to get the client’s ip address in your python code in DJANGO. You might have tried the ‘REMOTE_ADDR’ key in the request object but it always returns 127.0.0.1. To fix this, you can use the ‘HTTP_X_FORWARDED_FOR’ variable like so: from django.http import HttpRequest def mypage(request): client_address = request.META['HTTP_X_FORWARDED_FOR'] .... The […]

Continue reading...