Python django.views.generic.TemplateView.as_view() Examples

The following are 30 code examples of django.views.generic.TemplateView.as_view(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.views.generic.TemplateView , or try the search function .
Example #1
Source File: auth.py    From zulip with Apache License 2.0 6 votes vote down vote up
def config_error_view(request: HttpRequest, error_category_name: str) -> HttpResponse:
    contexts = {
        'apple': {'social_backend_name': 'apple', 'has_markdown_file': True},
        'google': {'social_backend_name': 'google', 'has_markdown_file': True},
        'github': {'social_backend_name': 'github', 'has_markdown_file': True},
        'gitlab': {'social_backend_name': 'gitlab', 'has_markdown_file': True},
        'ldap': {'error_name': 'ldap_error_realm_is_none'},
        'dev': {'error_name': 'dev_not_supported_error'},
        'saml': {'social_backend_name': 'saml'},
        'smtp': {'error_name': 'smtp_error'},
        'backend_disabled': {'error_name': 'remoteuser_error_backend_disabled'},
        'remote_user_header_missing': {'error_name': 'remoteuser_error_remote_user_header_missing'},
    }

    return TemplateView.as_view(template_name='zerver/config_error.html',
                                extra_context=contexts[error_category_name])(request) 
Example #2
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_invalid_keyword_argument(self):
        """
        View arguments must be predefined on the class and can't
        be named like a HTTP method.
        """
        msg = (
            "You tried to pass in the %s method name as a keyword argument "
            "to SimpleView(). Don't do that."
        )
        # Check each of the allowed method names
        for method in SimpleView.http_method_names:
            with self.assertRaisesMessage(TypeError, msg % method):
                SimpleView.as_view(**{method: 'value'})

        # Check the case view argument is ok if predefined on the class...
        CustomizableView.as_view(parameter="value")
        # ...but raises errors otherwise.
        msg = (
            "CustomizableView() received an invalid keyword 'foobar'. "
            "as_view only accepts arguments that are already attributes of "
            "the class."
        )
        with self.assertRaisesMessage(TypeError, msg):
            CustomizableView.as_view(foobar="value") 
Example #3
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_options(self):
        """
        Views respond to HTTP OPTIONS requests with an Allow header
        appropriate for the methods implemented by the view class.
        """
        request = self.rf.options('/')
        view = SimpleView.as_view()
        response = view(request)
        self.assertEqual(200, response.status_code)
        self.assertTrue(response['Allow']) 
Example #4
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_include_urlencoded_args(self):
        "GET arguments can be URL-encoded when included in the redirected URL"
        response = RedirectView.as_view(url='/bar/', query_string=True)(
            self.rf.get('/foo/?unicode=%E2%9C%93'))
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, '/bar/?unicode=%E2%9C%93') 
Example #5
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_redirect_when_meta_contains_no_query_string(self):
        "regression for #16705"
        # we can't use self.rf.get because it always sets QUERY_STRING
        response = RedirectView.as_view(url='/bar/')(self.rf.request(PATH_INFO='/foo/'))
        self.assertEqual(response.status_code, 302) 
Example #6
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_direct_instantiation(self):
        """
        It should be possible to use the view without going through .as_view()
        (#21564).
        """
        view = RedirectView()
        response = view.dispatch(self.rf.head('/foo/'))
        self.assertEqual(response.status_code, 410) 
Example #7
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_redirect_PATCH(self):
        "Default is a temporary redirect"
        response = RedirectView.as_view(url='/bar/')(self.rf.patch('/foo/'))
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, '/bar/') 
Example #8
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_redirect_HEAD(self):
        "Default is a temporary redirect"
        response = RedirectView.as_view(url='/bar/')(self.rf.head('/foo/'))
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, '/bar/') 
Example #9
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_redirect_POST(self):
        "Default is a temporary redirect"
        response = RedirectView.as_view(url='/bar/')(self.rf.post('/foo/'))
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, '/bar/') 
Example #10
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_named_url_pattern(self):
        "Named pattern parameter should reverse to the matching pattern"
        response = RedirectView.as_view(pattern_name='artist_detail')(self.rf.get('/foo/'), pk=1)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['Location'], '/detail/artist/1/') 
Example #11
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_parameter_substitution(self):
        "Redirection URLs can be parameterized"
        response = RedirectView.as_view(url='/bar/%(object_id)d/')(self.rf.get('/foo/42/'), object_id=42)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, '/bar/42/') 
Example #12
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_redirect_OPTIONS(self):
        "Default is a temporary redirect"
        response = RedirectView.as_view(url='/bar/')(self.rf.options('/foo/'))
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, '/bar/') 
Example #13
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_calling_parent_setup_error(self):
        class TestView(View):
            def setup(self, request, *args, **kwargs):
                pass  # Not calling supre().setup()

        msg = (
            "TestView instance has no 'request' attribute. Did you override "
            "setup() and forget to call super()?"
        )
        with self.assertRaisesMessage(AttributeError, msg):
            TestView.as_view()(self.rf.get('/')) 
Example #14
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_overridden_setup(self):
        class SetAttributeMixin:
            def setup(self, request, *args, **kwargs):
                self.attr = True
                super().setup(request, *args, **kwargs)

        class CheckSetupView(SetAttributeMixin, SimpleView):
            def dispatch(self, request, *args, **kwargs):
                assert hasattr(self, 'attr')
                return super().dispatch(request, *args, **kwargs)

        response = CheckSetupView.as_view()(self.rf.get('/'))
        self.assertEqual(response.status_code, 200) 
Example #15
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_args_kwargs_request_on_self(self):
        """
        Test a view only has args, kwargs & request once `as_view`
        has been called.
        """
        bare_view = InstanceView()
        view = InstanceView.as_view()(self.rf.get('/'))
        for attribute in ('args', 'kwargs', 'request'):
            self.assertNotIn(attribute, dir(bare_view))
            self.assertIn(attribute, dir(view)) 
Example #16
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_options_for_get_and_post_view(self):
        """
        A view implementing GET and POST allows GET, HEAD, and POST.
        """
        request = self.rf.options('/')
        view = SimplePostView.as_view()
        response = view(request)
        self._assert_allows(response, 'GET', 'HEAD', 'POST') 
Example #17
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_options_for_get_view(self):
        """
        A view implementing GET allows GET and HEAD.
        """
        request = self.rf.options('/')
        view = SimpleView.as_view()
        response = view(request)
        self._assert_allows(response, 'GET', 'HEAD') 
Example #18
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_direct_instantiation(self):
        """
        It should be possible to use the view by directly instantiating it
        without going through .as_view() (#21564).
        """
        view = PostOnlyView()
        response = view.dispatch(self.rf.head('/'))
        self.assertEqual(response.status_code, 405) 
Example #19
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_dispatch_decoration(self):
        """
        Attributes set by decorators on the dispatch method
        are also present on the closure.
        """
        self.assertTrue(DecoratedDispatchView.as_view().is_decorated) 
Example #20
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_class_attributes(self):
        """
        The callable returned from as_view() has proper
        docstring, name and module.
        """
        self.assertEqual(SimpleView.__doc__, SimpleView.as_view().__doc__)
        self.assertEqual(SimpleView.__name__, SimpleView.as_view().__name__)
        self.assertEqual(SimpleView.__module__, SimpleView.as_view().__module__) 
Example #21
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_and_post(self):
        """
        Test a view which only allows both GET and POST.
        """
        self._assert_simple(SimplePostView.as_view()(self.rf.get('/')))
        self._assert_simple(SimplePostView.as_view()(self.rf.post('/')))
        self.assertEqual(SimplePostView.as_view()(
            self.rf.get('/', REQUEST_METHOD='FAKE')
        ).status_code, 405) 
Example #22
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_head_no_get(self):
        """
        Test a view which supplies no GET method responds to HEAD with HTTP 405.
        """
        response = PostOnlyView.as_view()(self.rf.head('/'))
        self.assertEqual(response.status_code, 405) 
Example #23
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_and_head(self):
        """
        Test a view which supplies a GET method also responds correctly to HEAD.
        """
        self._assert_simple(SimpleView.as_view()(self.rf.get('/')))
        response = SimpleView.as_view()(self.rf.head('/'))
        self.assertEqual(response.status_code, 200) 
Example #24
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_only(self):
        """
        Test a view which only allows GET doesn't allow other methods.
        """
        self._assert_simple(SimpleView.as_view()(self.rf.get('/')))
        self.assertEqual(SimpleView.as_view()(self.rf.post('/')).status_code, 405)
        self.assertEqual(SimpleView.as_view()(
            self.rf.get('/', REQUEST_METHOD='FAKE')
        ).status_code, 405) 
Example #25
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_no_init_args(self):
        """
        A view can't be accidentally instantiated before deployment
        """
        msg = 'as_view() takes 1 positional argument but 2 were given'
        with self.assertRaisesMessage(TypeError, msg):
            SimpleView.as_view('value') 
Example #26
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_no_init_kwargs(self):
        """
        A view can't be accidentally instantiated before deployment
        """
        msg = 'This method is available only on the class, not on instances.'
        with self.assertRaisesMessage(AttributeError, msg):
            SimpleView(key='value').as_view() 
Example #27
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_direct_instantiation(self):
        """
        It should be possible to use the view without going through .as_view()
        (#21564).
        """
        view = RedirectView()
        response = view.dispatch(self.rf.head('/foo/'))
        self.assertEqual(response.status_code, 410) 
Example #28
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_redirect_when_meta_contains_no_query_string(self):
        "regression for #16705"
        # we can't use self.rf.get because it always sets QUERY_STRING
        response = RedirectView.as_view(url='/bar/')(self.rf.request(PATH_INFO='/foo/'))
        self.assertEqual(response.status_code, 302) 
Example #29
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_redirect_DELETE(self):
        "Default is a temporary redirect"
        response = RedirectView.as_view(url='/bar/')(self.rf.delete('/foo/'))
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, '/bar/') 
Example #30
Source File: test_base.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_redirect_PUT(self):
        "Default is a temporary redirect"
        response = RedirectView.as_view(url='/bar/')(self.rf.put('/foo/'))
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, '/bar/')