本文整理匯總了Python中django.views.generic.CreateView方法的典型用法代碼示例。如果您正苦於以下問題:Python generic.CreateView方法的具體用法?Python generic.CreateView怎麽用?Python generic.CreateView使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.views.generic
的用法示例。
在下文中一共展示了generic.CreateView方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: dispatch
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def dispatch(self, request, *args, **kwargs):
"""
Permission check for create and general views.
The context is determined according to the parent object, which is expected
to be already retrieved by previous dispatch() methods, and stored in the
auth_base keyword argument.
"""
if getattr(self, 'exact_role', None) == ANONYMOUS or self.minimum_role == ANONYMOUS:
self.allow_anonymous = True
if not request.user.is_authenticated and not self.allow_anonymous:
self.role = VISITOR
return self.handle_no_permission() # authorization implies a logged-in user
if 'auth_base' in kwargs:
object = kwargs['auth_base']
self._auth_verify(object, context_omitted=object is None)
elif isinstance(self, generic.CreateView):
raise ImproperlyConfigured(
"Creation base not found. Make sure {}'s auth_base is accessible by "
"AuthMixin as a dispatch kwarg.".format(self.__class__.__name__)
)
return super().dispatch(request, *args, **kwargs)
示例2: _get_object
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def _get_object(self, cls, attr):
# CreateView supplies a defect get_object method and has to be excluded
if hasattr(self, 'get_object') \
and not isinstance(self, generic.CreateView):
try:
object = self.get_object()
if isinstance(object, cls):
return object
if hasattr(object, attr):
return getattr(object, attr)
except Http404:
return None
except AttributeError:
return None
return None
示例3: get_create_class_view
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def get_create_class_view(self):
name = '{0}_{1}'.format(self.get_name(), 'CreateView')
options_dict = {
'kind': 'create',
'model': self.model,
'fields': '__all__',
}
if hasattr(self, 'form_template_name') and self.form_template_name:
options_dict['template_name'] = self.form_template_name
if hasattr(self, 'form_class') and self.form_class:
options_dict['form_class'] = self.form_class
options_dict['fields'] = None
parent_classes_list = [FallbackTemplateMixin]
parent_classes_list.extend(self.create_mixins)
parent_classes_list.append(self.create_view_class)
klazz = type(name, tuple(parent_classes_list), options_dict )
klazz.get_context_data = self.get_get_context_data(klazz)
return klazz
示例4: get_context_data
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def get_context_data(self, **kwargs):
context = super(generic.CreateView, self).get_context_data(**kwargs)
context['cities'] = Job.cities
context['categories'] = Job.categories
return context
示例5: get_context_data
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def get_context_data(self, **kwargs):
"""Overriding get_context_data to add additional context."""
context = super(CreateView, self).get_context_data(**kwargs)
# Provides the base template to extend from
context['template_extends'] = self.template_extends
return context
示例6: get
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def get(self, request, *args, **kwargs):
""" Creates form with initial data, and renders JSON response """
super(CreateView, self).get(request, *args, **kwargs)
self.request = request
form = self.get_form()
return self.renderJsonResponse(request, form)
示例7: get_success_url
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def get_success_url(self):
# XXX: This is unused but enforced by `CreateView`
return reverse("pootle-user-stats", kwargs=self.kwargs)
示例8: post
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def post(self, request, *args, **kwargs):
try:
self.object = self.get_object()
success_url = self.get_success_url()
self.object.deleted = False
self.object.save()
return HttpResponseRedirect(success_url)
except:
pass
return super(CreateView, self).post(request, *args, **kwargs)
示例9: test_views_have_correct_parent_class
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def test_views_have_correct_parent_class(self):
self.assertEquals(self.list_view.__bases__[-1].__name__, "ListView")
self.assertEquals(self.create_view.__bases__[-1].__name__, "CreateView")
self.assertEquals(self.update_view.__bases__[-1].__name__, "UpdateView")
self.assertEquals(self.delete_view.__bases__[-1].__name__, "DeleteView")
self.assertEquals(self.detail_view.__bases__[-1].__name__, "DetailView")
示例10: test_predefined_view_type
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def test_predefined_view_type(self):
class TestView(AutoPermissionRequiredMixin, CreateView):
model = self.model
fields = ()
self.assertEqual(TestView.as_view()(self.req).status_code, 200)
示例11: test_custom_view_type
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def test_custom_view_type(self):
class CustomViewMixin:
pass
class TestView(AutoPermissionRequiredMixin, CustomViewMixin, CreateView):
model = self.model
fields = ()
permission_type_map = [
(CustomViewMixin, "unknown_perm")
] + AutoPermissionRequiredMixin.permission_type_map
raise_exception = True
with self.assertRaises(PermissionDenied):
TestView.as_view()(self.req)
示例12: test_overwrite_perm_type
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def test_overwrite_perm_type(self):
class TestView(AutoPermissionRequiredMixin, CreateView):
model = self.model
fields = ()
permission_type = "unknown"
raise_exception = True
with self.assertRaises(PermissionDenied):
TestView.as_view()(self.req)
示例13: test_disable_perm_checking
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def test_disable_perm_checking(self):
class TestView(AutoPermissionRequiredMixin, CreateView):
model = self.model
fields = ()
permission_type = None
self.assertEqual(TestView.as_view()(self.req).status_code, 200)
示例14: test_permission_required_passthrough
# 需要導入模塊: from django.views import generic [as 別名]
# 或者: from django.views.generic import CreateView [as 別名]
def test_permission_required_passthrough(self):
class TestView(AutoPermissionRequiredMixin, CreateView):
model = self.model
fields = ()
permission_required = "testapp.unknown_perm"
raise_exception = True
with self.assertRaises(PermissionDenied):
TestView.as_view()(self.req)