本文整理匯總了Python中django.views.generic.edit.CreateView方法的典型用法代碼示例。如果您正苦於以下問題:Python edit.CreateView方法的具體用法?Python edit.CreateView怎麽用?Python edit.CreateView使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.views.generic.edit
的用法示例。
在下文中一共展示了edit.CreateView方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: post
# 需要導入模塊: from django.views.generic import edit [as 別名]
# 或者: from django.views.generic.edit import CreateView [as 別名]
def post(self, request, section, pk):
form = MessageForm(request.POST)
if form.is_valid():
form = form.save(commit=False)
form.user = request.user
form.topic_id = pk
form.save()
return redirect("/forum/section/{}/{}/?page=last".format(section, pk))
# class MessageCreate(LoginRequiredMixin, CreateView):
# """Создание темы на форуме"""
# model = Message
# form_class = MessageForm
# template_name = 'forum/topic-detail.html'
#
# def form_valid(self, form):
# form.instance.user = self.request.user
# form.instance.topic_id = self.kwargs.get("pk")
# form.save()
# return super().form_valid(form)
示例2: get_create_view_class
# 需要導入模塊: from django.views.generic import edit [as 別名]
# 或者: from django.views.generic.edit import CreateView [as 別名]
def get_create_view_class(self):
class UCreateView(CreateView):
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.save()
return HttpResponseRedirect(self.get_success_url())
return UCreateView
示例3: post
# 需要導入模塊: from django.views.generic import edit [as 別名]
# 或者: from django.views.generic.edit import CreateView [as 別名]
def post(self, request, pk) :
make = get_object_or_404(self.model, pk=pk)
make.delete()
return redirect(self.success_url)
# Take the easy way out on the main table
# These views do not need a form because CreateView, etc.
# Build a form object dynamically based on the fields
# value in the constructor attributes
示例4: form_valid
# 需要導入模塊: from django.views.generic import edit [as 別名]
# 或者: from django.views.generic.edit import CreateView [as 別名]
def form_valid(self, form):
form.instance.creator = self.request.user
# NOTE: this is necessary here even though it is a CreateView, because
# "ValueError: "<Project: test_project>" needs to have a value for
# field "project" before this many-to-many relationship can be used."
form.instance.save()
form.instance.manager.add(self.request.user)
# per default a user follows the project he created
follow_project(self.request.user, form.instance)
# and every developer follows the project, too
for dev in form.cleaned_data["developer"]:
follow_project(dev, form.instance)
return super(ProjectCreateView, self).form_valid(form)
示例5: form_valid
# 需要導入模塊: from django.views.generic import edit [as 別名]
# 或者: from django.views.generic.edit import CreateView [as 別名]
def form_valid(self, form):
form.instance.project = Project.objects.get(name_short=self.kwargs.get('project'))
form.instance.creator = self.request.user
# NOTE: this is necessary here even though it is a CreateView, because
# otherwise the reversematch in signals.create.send fails
form.instance.save()
signals.create.send(sender=self.model, instance=form.instance, user=self.request.user)
return super(IssueCreateView, self).form_valid(form)
示例6: test_create_view_with_restricted_fields
# 需要導入模塊: from django.views.generic import edit [as 別名]
# 或者: from django.views.generic.edit import CreateView [as 別名]
def test_create_view_with_restricted_fields(self):
class MyCreateView(CreateView):
model = Author
fields = ['name']
self.assertEqual(list(MyCreateView().get_form_class().base_fields), ['name'])
示例7: test_create_view_all_fields
# 需要導入模塊: from django.views.generic import edit [as 別名]
# 或者: from django.views.generic.edit import CreateView [as 別名]
def test_create_view_all_fields(self):
class MyCreateView(CreateView):
model = Author
fields = '__all__'
self.assertEqual(list(MyCreateView().get_form_class().base_fields), ['name', 'slug'])
示例8: test_create_view_without_explicit_fields
# 需要導入模塊: from django.views.generic import edit [as 別名]
# 或者: from django.views.generic.edit import CreateView [as 別名]
def test_create_view_without_explicit_fields(self):
class MyCreateView(CreateView):
model = Author
message = (
"Using ModelFormMixin (base class of MyCreateView) without the "
"'fields' attribute is prohibited."
)
with self.assertRaisesMessage(ImproperlyConfigured, message):
MyCreateView().get_form_class()
示例9: test_define_both_fields_and_form_class
# 需要導入模塊: from django.views.generic import edit [as 別名]
# 或者: from django.views.generic.edit import CreateView [as 別名]
def test_define_both_fields_and_form_class(self):
class MyCreateView(CreateView):
model = Author
form_class = AuthorForm
fields = ['name']
message = "Specifying both 'fields' and 'form_class' is not permitted."
with self.assertRaisesMessage(ImproperlyConfigured, message):
MyCreateView().get_form_class()