diff --git a/djadmin2/core.py b/djadmin2/core.py index 5adafdf..1d29aa9 100644 --- a/djadmin2/core.py +++ b/djadmin2/core.py @@ -27,6 +27,7 @@ class Admin2(object): admin site. """ index_view = views.IndexView + login_view = views.LoginView app_index_view = views.AppIndexView api_index_view = apiviews.IndexAPIView @@ -142,6 +143,7 @@ class Admin2(object): 'registry': self.registry, 'app_verbose_names': self.app_verbose_names, 'apps': self.apps, + 'login_view': self.login_view, } def get_app_index_kwargs(self): diff --git a/djadmin2/tests/templates/djadmin2theme_bootstrap3/custom_login_template.html b/djadmin2/tests/templates/djadmin2theme_bootstrap3/custom_login_template.html new file mode 100644 index 0000000..b457334 --- /dev/null +++ b/djadmin2/tests/templates/djadmin2theme_bootstrap3/custom_login_template.html @@ -0,0 +1,32 @@ +{% extends "djadmin2theme_bootstrap3/base.html" %} +{% load i18n staticfiles admin2_tags %} + +{% block navbar %}{% endblock navbar %} +{% block breacrumbs %}{% endblock breacrumbs %} + +{% block page_header %}{% endblock page_header %} + +{% block content %} +
+
+
+ +
+
+
+{% endblock content %} diff --git a/djadmin2/tests/test_views.py b/djadmin2/tests/test_views.py index 1e25e76..babe1bc 100644 --- a/djadmin2/tests/test_views.py +++ b/djadmin2/tests/test_views.py @@ -1,4 +1,6 @@ -from django.test import TestCase +from django.core.urlresolvers import reverse +from django.test import TestCase, override_settings +from django.utils.encoding import force_text from .. import views @@ -16,3 +18,11 @@ class AdminViewTest(TestCase): def test_name(self): self.assertEquals(self.admin_view.name, 'admin-view') + + +@override_settings(ROOT_URLCONF='djadmin2.tests.urls') +class CustomLoginViewTest(TestCase): + + def test_view_ok(self): + response = self.client.get(reverse("admin2:dashboard")) + self.assertInHTML('

Custom login view

', force_text(response.content)) diff --git a/djadmin2/tests/urls.py b/djadmin2/tests/urls.py new file mode 100644 index 0000000..8b01b3f --- /dev/null +++ b/djadmin2/tests/urls.py @@ -0,0 +1,20 @@ +from __future__ import unicode_literals + +from django.conf import settings +from django.conf.urls import include, url +from django.conf.urls.static import static + +from djadmin2.site import djadmin2_site + +from djadmin2.views import LoginView + + +class CustomLoginView(LoginView): + default_template_name = "custom_login_template.html" + +djadmin2_site.login_view = CustomLoginView +djadmin2_site.autodiscover() + +urlpatterns = [ + url(r'^admin2/', include(djadmin2_site.urls)), +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/djadmin2/viewmixins.py b/djadmin2/viewmixins.py index cf7888e..df09c51 100644 --- a/djadmin2/viewmixins.py +++ b/djadmin2/viewmixins.py @@ -76,6 +76,7 @@ class Admin2Mixin(PermissionMixin): model_admin = None model_name = None app_label = None + login_view = None index_path = reverse_lazy('admin2:dashboard') @@ -101,8 +102,6 @@ class Admin2Mixin(PermissionMixin): def dispatch(self, request, *args, **kwargs): if self.is_user(request): - from .views import LoginView - if request.path == reverse('admin2:logout'): return HttpResponseRedirect(self.index_path) @@ -110,8 +109,7 @@ class Admin2Mixin(PermissionMixin): extra = { 'next': request.GET.get('next', self.index_path) } - return LoginView().dispatch(request, extra_context=extra, - *args, **kwargs) + return self.login_view().dispatch(request, extra_context=extra, *args, **kwargs) return super(Admin2Mixin, self).dispatch(request, *args, **kwargs) diff --git a/docs/ref/views.rst b/docs/ref/views.rst index 463a129..a21c26c 100644 --- a/docs/ref/views.rst +++ b/docs/ref/views.rst @@ -44,4 +44,39 @@ In your Django project's root URLconf module (``urls.py``) modify the code to in In real projects the new IndexView would likely be placed into a ``views.py`` module. -.. note:: Considering that dashboard is more intuitive of a name, perhaps the ``IndexView`` should be renamed ``DashboardView``? \ No newline at end of file +.. note:: Considering that dashboard is more intuitive of a name, perhaps the ``IndexView`` should be renamed ``DashboardView``? + +Customizing the Login view +========================== + +The login view could also be customized. + +In your Django project's root URLconf module (``urls.py``) modify the code to include the commented code before the ``djadmin2.default.autodiscover()``: + +.. code-block:: python + + from django.conf.urls import patterns, include, url + + from djadmin2.site import djadmin2_site + from djadmin2.views import LoginView + + + ######### Begin django-admin2 customization code + # Create a new django-admin2 index view + class CustomLoginView(LoginView): + + # specify the template + default_template_name = "custom_login_template.html" + + # override the default index_view + djadmin2_site.login_view = CustomLoginView + ######### end django-admin2 customization code + + djadmin2_site.autodiscover() + + urlpatterns = patterns('', + url(r'^admin2/', include(djadmin2_site.urls)), + # ... Place the rest of the project URLs here + ) + +In real projects the new LoginView would likely be placed into a ``views.py`` module. diff --git a/example/example/urls.py b/example/example/urls.py index 89f0c48..a247fd4 100644 --- a/example/example/urls.py +++ b/example/example/urls.py @@ -18,4 +18,3 @@ urlpatterns = [ url(r'^blog/detail(?P\d+)/$', BlogDetailView.as_view(template_name="blog/blog_detail.html"), name='blog_detail'), url(r'^$', BlogListView.as_view(template_name="blog/home.html"), name='home'), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) -