本文整理汇总了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)