Hello All, First off, let me start by saying I love the Django framework (you guys got me into listening to the music as well). So, the relevant message there is: "Thank you to everyone who's made this happen!"
I have a feature request that I've already implemented on my end that I'd like to propose for Django. A search of both the Django Developers and Django Users lists turned up some slightly similar ideas but nothing exactly as I'm proposing here (that I know of) so I'll go ahead and present this. The code follows below. My questions are: 1. Does anyone else see this as useful? 2. If so, how would you like me to proceed? SUMMARY OF PROPOSAL: For Django application modules, I find it easier to have the URL information for a view coupled with the function that is going to display that view. Why? This further supports the DRY (Don't Repeat Yourself) principle in my mind because you don't have to type both the view function definition as well as the "mapping" for that view to the relevant URL regular expression. My mind also works better such that it makes sense to keep view and URL mappings in one logical place as opposed to jumping back and forth between two files. Following is an example and the code that runs it EXAMPLE: > django-admin.py startproject mysite > cd mysite > python manage.py startapp myapp # make changes to files as stated below > python manage.py runserver # go to: # http://localserver:8000/ # http://localserver:8000/about/ # http://localserver:8000/contacts/ # http://localserver:8000/links/ change mysite/urls.py to: from django.conf.urls.defaults import * urlpatterns = patterns('', (r'', include('myapp.views')), ) change mysite/myapp/views.py to # Create your views here. from addon_django.helpers import * from django.http import HttpResponse _URLS=[] # uses python decorator syntax to add to a list of arguments # to be used to call django.conf.urls.defaults.patterns as in a normal urls.py file @bind_to_url(_URLS,r'^$') def show_index(request): return HttpResponse('index') @bind_to_url(_URLS,r'^about/$') def show_about(request): return HttpResponse('about') @bind_to_urls(_URLS,[r'^contact/$',r'^links/$'], [{'text':'My contact page'},{'text':'My links'}]) def show_simple_text(request,text): return HttpResponse(text) # creates the urlpatterns variable ala urls.py urlpatterns = get_urlpatterns(_URLS) The helpers.py module that makes the above syntax possible follows. I've got it sitting in a folder called addon_django on my python path (complete with __init__.py inside): # module: addon_django.helpers from django.conf.urls.defaults import * __all__= ["bind_to_url","bind_to_urls","bind_other_url","get_urlpatterns"] def bind_to_url(_URLS,url_pattern,param_dict=None): """returns a function that processes URL information associated with a view function. Binds URL pattern information, url_pattern (a regular expression), and optional parameter dictionary, param_dict, to a global variable _URLS """ def bind_func(f): if param_dict is None: _URLS.append( (url_pattern,f,) ) else: _URLS.append( (url_pattern,f,param_dict,) ) return f return bind_func def bind_to_urls(_URLS,url_pattern_list,param_dict_list=None): """returns a function that processes a function (DECORATOR) Exactly like bind_to_url but takes a list of URL patterns and a list of optional parameter dictionaries (param_dict_list) and adds the information to the global list _URLS. Note: if some url patterns take a parameter dictionary and some do not, the format of param_dict_list should be: bind_to_urls(_URLS,url_pattern_list=[r'^$',r'^index/$'], param_dict_list=[{'param1':1},None]) """ def bind_func(f): if param_dict_list is None: for url_pattern in url_pattern_list: _URLS.append( (url_pattern,f,) ) else: for (url_pattern,param_dict) in zip (url_pattern_list,param_dict_list): if param_dict is None: _URLS.append( (url_pattern,f,) ) else: _URLS.append( (url_pattern,f,param_dict,) ) return f return bind_func def bind_other_url(_URLS,url_pattern, func, param_dict=None): """modifies global list _URLS to add argument information to patterns function (see get_urlpatterns) For binding "other" things to the URL list (e.g., generic views) """ if param_dict is not None: _URLS.append( (url_pattern, func,) ) else: _URLS.append( (url_pattern, func, param_dict,) ) def get_urlpatterns(_URLS,BASE_PATTERN=''): """returns urlpatterns variable Creates the variable urlpatterns using list _URLS to supply arguments to django.conf.urls.defaults.patterns(). BASE_PATTERN is the first argument to the patterns function. BASE_PATTERN should be '' if you are binding callables to urls. That is, don't specify a base pattern if you will also pass in a function/callable object. """ return patterns(BASE_PATTERN,*_URLS) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---
