> >> 7. Requests in forms > > >> I suspect the answer on this one will be no for the simple reason of > >> backwards compatibility. A request attribute is only really useful if > >> it is ubiquitous, and there's no way to make it ubiquitous without > >> breaking backwards compatibility. Truth be told, having the request > >> around would have made CSRF a lot easier, but it wasn't so we had to > >> fall back on {% csrf_token %}. > > >> This is also something that can be implemented in user-space by > >> subclassing a form and enforcing the request argument. So, call this a > >> -0 from me. > > > I think it should not be backward-incompatible - init of forms and > > formsets would have last argument requiest with default value None. So > > current code will continue to work. > > > But I fully agree that it could be solved by subclass which would save > > the request. But if it will be quite common for init methods to accept > > request arguments, new generic views could set this argument. > > Otherwise everybody who would like to use request in forms and generic > > views has to subclass forms and generic views, they could not be > > enhanced independently. > > As soon as you make the parameter optional, it ceases to be > ubiquitous, which significantly reduces it's usefulness. The only way > I can see around this is to introduce a "RequestForm", but I'm not > sure if I'm wild about the idea of complicating the decision about > which form base class you should use.
It is quite similar to alternative solution I want to use if request in django.forms would be rejected. 1. Implement very simple RequestForm: class RequestForm(object): def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super(RequestForm, self).__init__(*args, **kwargs) 2. Check RequestForm in my general views: if isinstance(form_class, RequestForm): form = form_class(request.POST, request.FILES, request=request) else: form = form_class(request.POST, request.FILES) 3. Subclass RequestForm if request is needed: class MyForm(RequestForm, forms.ModelForm): def do_something(self): if self.request.user.is_authenticated: ... RequestForm is not a form (maybe better name is needed) so it is no problem to use it for common forms and model forms. Is any chance to accept something similar to Django? Otherwise authors of reusable general views would not be able to check if they can set request. > > Yours, > Russ Magee %-) -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.