如何为模型创建基于通用类的创建视图?
我想做的是功能视图的 Django 样板。非常感谢这里的任何帮助,因为文档显示了模板视图和列表视图的示例,但我发现基于模型的通用视图很少。我在文档中缺少示例吗?
我有一个代表日历中条目的模型。有一个外键指向拥有该条目的另一个对象(不是用户)。我想要做的只是创建条目,确保正确设置条目的外键,然后将用户返回到适当的日历页面。
不过,我不知道基于类的通用视图如何接收其 URL 参数,并且不清楚如何设置 success_url 以便它重用最初传递到创建 URL 的 id。再次预先感谢您的帮助。
本质上,我要问的是,基于类的通用视图相当于以下内容:
def create_course_entry(request, class_id):
'''Creates a general calendar entry.'''
if request.method == 'POST':
form = CourseEntryForm(request.POST)
if form.is_valid():
new_entry = form.save(commit=False)
new_entry.course = Class.objects.get(pk=class_id)
new_entry.full_clean()
new_entry.save()
return HttpResponseRedirect('/class/%s/calendar/' % class_id)
else:
form = CourseEntryForm()
return render_to_response('classes/course_entry_create.html',
{ 'class_id': class_id, 'form': form, },
context_instance=RequestContext(request))
What I'm trying to do is Django boilerplate for functional views. Any help here is very much appreciated, as the docs show examples for the template view and list view, but I've found very little for the model-based generic views. Am I missing an example in the docs?
I have a model that represents an entry in a calendar. There's a foreign key to another object (not a user) that owns the entry. What I want to do is simply to create the entry, ensuring that the entry's foreign key is properly set and then return the user to the appropriate calendar page.
I don't know, though, how class-based generic views receive their URL arguments and I'm not clear on how to set the success_url so that it reuses the id that was originally passed to the creation URL. Again, thank you in advance for your help.
What I'm asking, essentially, is, what is the class-based generic view equivalent of the following:
def create_course_entry(request, class_id):
'''Creates a general calendar entry.'''
if request.method == 'POST':
form = CourseEntryForm(request.POST)
if form.is_valid():
new_entry = form.save(commit=False)
new_entry.course = Class.objects.get(pk=class_id)
new_entry.full_clean()
new_entry.save()
return HttpResponseRedirect('/class/%s/calendar/' % class_id)
else:
form = CourseEntryForm()
return render_to_response('classes/course_entry_create.html',
{ 'class_id': class_id, 'form': form, },
context_instance=RequestContext(request))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以对
edit.CreateView
通用视图进行子类化,在dispatch()
方法中设置类/课程,并通过覆盖form_valid()
来保存它code> 方法:如果您不自定义
CourseEntryForm
ModelForm
,则可以省略form_class
属性。不幸的是,不可能在
form_valid()
方法中调用super()
- 因为它的编写方式意味着对象将被再次保存。如果您需要模板上下文中的类(课程?)实例,那么您可以将其添加到
get_context_data()
方法中:You could subclass the
edit.CreateView
generic view, set the class/course in thedispatch()
method, and save this by overriding theform_valid()
method:If you're not customising the
CourseEntryForm
ModelForm
, then you can leave out theform_class
property.Unfortunately, it is not possible to call
super()
in theform_valid()
method - due to the way it has been written would mean the object would be saved again.If you need the Class (course?) instance in the template context, then you can add this in the
get_context_data()
method:Matt Austin 的答案的替代方法可能是重写
get_form
方法:这样,
.course
位于上下文中的CourseEntry
实例上,并且在 POST 保存表单时创建的实例上。An alternative to Matt Austin's answer might be to override the
get_form
method:This way,
.course
is on theCourseEntry
instance in the context, and on the instance created when the form is saved upon POST.