source code
- forms.Widget forms/widgets.py
- forms.Field forms/fields.py
- ModelForm forms/models.py class ModelForm(six.with_metaclass(ModelFormMetaclass, BaseModelForm)):
- Model db/models/base.py class Model(six.with_metaclass(ModelBase)):
app namespace and instance name space
introduction
generic explaining.
https://docs.djangoproject.com/en/1.5/topics/http/urls/#topics-http-reversing-url-namespaces
in admin: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#hooking-adminsite-instances-into-your-urlconf
url(r'^help/', include('apps.help.urls', namespace='foo', app_name='app_poll')),
app_name : means an django project can have multiple applications. this is application namespace.
namespace: means an app can have multiple instances deployed. for example, you have a photo sharing system, you can deploy it for different customers, one for car, the other for fruit. this is call instance namepsaces
django.core.urlresolvers.resolve can be used:
r = resolve(request.path) r.app_name # the app name r.namespace # the the currently active instance
An example of instance namespace:
how to use it.
when we refer it, in template:
<li><a href="{% url 'foo:detail' poll.id %}">{{ poll.question }}</a></li>
dynamic generating
in above we still have to hard code foo:detail. in fact, it is possible to dynamic generating it as:
class AlbumCreateView(TemplateView): template_name = 'path/to/my/template.html' def render_to_response(self, context, **response_kwargs): response_kwargs['current_app'] = resolve(self.request.path).namespace return super(AlbumPageView, self).render_to_response(context, **response_kwargs)
Another approach is add it as argument in url
http://stackoverflow.com/questions/1919328/how-do-you-use-django-url-namespaces?rq=1
for reverse:
HttpResponseRedirect(reverse('foo:detail', args=(id,)))
note, if you use 'app_poll:detail', django will resolve to the current instance in template. but in reverse(), you have to supply current_app parameter to identify the
right instance, such as:
reverse('app_poll:details', current_app=resolve(self.request.path).namespace))
if app_name is not defined in urls.py, then you can write it like:
reverse('details', current_app=resolve(self.request.path).namespace))
customize field and widget
http://tothinkornottothink.com/post/10815277049/django-forms-i-custom-fields-and-widgets-in-detail
http://excess.org/article/2011/09/django-widgets-formfields-modelfields/
https://gist.github.com/armonge/1089705
http://www.huyng.com/posts/django-custom-form-widget-for-dictionary-and-tuple-key-value-pairs/