Hi All,
during my analyzing and making proposal for django best practices updates
(class based view etc) I found this ticket
related https://code.djangoproject.com/ticket/16256 [though closed by a
core dev I think this should be addressed to resolve some issues in admin
as per https://code.djangoproject.com/ticket/17209]
Here goes some of my initial proposal plan I will be updating regularly to
complete the proposal in a gist and share that again. before that please
give some feedback :)
Background
Over the years, as Django has evolved, the idea of what constitutes "best
practice" has also evolved. However, some parts of Django haven't kept up
with those best practices. For example, contrib apps do not use class based
views.
In short, Django has been bad at eating it's own dogfood. The contents of
contrib should be audited and updated to make sure it meets current best
practices.
For updating best practices first think to consider is to convert the
functional views of django contrib apps to class based views where possible
and necessary. To do so first app to consider is django contrib auth.
contrib-auth views.py now have function based views and they are
login logout logout_then_login redirect_to_login password_reset
password_reset_done password_reset_confirm password_reset_complete
password_change password_change_done
and in urls.py url mapping for function based views as follows
urlpatterns = [ url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'), url(r'^password_change/$',
views.password_change, name='password_change'),
url(r'^password_change/done/$', views.password_change_done,
name='password_change_done'), url(r'^password_reset/$',
views.password_reset, name='password_reset'),
url(r'^password_reset/done/$', views.password_reset_done,
name='password_reset_done'),
url(r'^reset/(?P[0-9A-Za-z_-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
views.password_reset_confirm, name='password_reset_confirm'),
url(r'^reset/done/$', views.password_reset_complete,
name='password_reset_complete'), ]
and tests for this in tests/auth_tests/test_views.py
the views have to be converted into CBV and also the urls. and this should
be done in a backward compatible manner.
as an example password_change_done view is now implemented as below.
@login_required def password_change_done(request,
template_name='registration/password_change_done.html', current_app=None,
extra_context=None): context = { 'title': _('Password change successful'),
} if extra_context is not None: context.update(extra_context)
if current_app is not None:
request.current_app = current_app
return TemplateResponse(request, template_name, context)
this could be changed to cbv like below
class PasswordChangeDoneView(TemplateView):
template_name='registration/password_change_done.html' current_app=None
extra_context=None
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(Password_Change_Done, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(Password_Change_Done, self).get_context_data(**kwargs)
context.update({
'title': _('Password change successful'),
'current_app': self.current_app,
})
if self.extra_context is not None:
context.update(self.extra_context)
return context
<https://gist.github.com/auvipy/1da0d96f826bd8da4d47#for-backward-compatibility-the-following-way-can-be-followed>for
backward compatibility the following way can be followed
def password_change_done(request, *args, **kwargs):
return PasswordChangeDoneView.as_view(**kwargs)(request, *args,
**kwargs)
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/248fbc76-0804-4128-998a-bb4f17f2c98c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.