Previous Page Next Page

Q&A

Q.If I need to include only one extra variable, can I just specify the variable as a parameter after the view location in the URL pattern?
A.The extra options must be contained in a dictionary. You can add the dictionary to the URL pattern as follows:
 (r'^Blogs/', 'mySite.Blogs.views.index', {'maxSize':10}),

Q.Is there a way to get the absolute URL for a view in my Python code?
A.Yes, Django provides the django.core.urlresolvers.reverse(viewname, urlconf=None, args=None, kwargs=None) function that will return the absolute URL for a view function. The reverse() function accepts either a function reference or a string version of the name as the viewname argument. The reverse() function also accepts optional arguments that allow you to specify which Urlconf file to use and the args and kwargs that should be passed to the view in the absolute URL. For example, consider the following URL:
 (r'^People/Details/(?P<pID>\d+)/$', 'details'),

The following code allows you to retrieve the absolute URL for the iFriends.People.views.details view for a specific userID:

from django.core.urlresolvers import reverse
from iFriends.People.views import details
def getURL(userID):
    absoluteURL = reverse(details, args=[userID])

Previous Page Next Page