In Django 1.4 and before, I had views which looked like this:
在Django 1.4和之前,我有如下视图:
def myview(request, item_id):
item = get_object_or_404(Item, item_id)
if request.method == "GET":
return direct_to_template(request, "template.html",
{ 'form': ItemForm() })
elif request.method == "POST":
form = ItemForm(request.POST)
if form.is_valid():
return redirect("/")
else:
return direct_to_template(request, "template.html",
{ 'form': form })
I'd like to rewrite this to be compatible with Django's new class-based-view system utilizing TemplateView, but I'm not sure as to how to implement the methods. How do I migrate over to using a TemplateView
subclass to implement POST and GET?
我想重写它以与Django使用TemplateView的新的基于类的视图系统兼容,但是我不确定如何实现这些方法。如何迁移到使用TemplateView子类来实现POST和GET?
1 个解决方案
#1
1
You probably want to use a FormView:
您可能需要使用FormView:
class ItemFormView(FormView):
template_name = 'template.html'
form_class = ItemForm
success_url = '/'
#1
1
You probably want to use a FormView:
您可能需要使用FormView:
class ItemFormView(FormView):
template_name = 'template.html'
form_class = ItemForm
success_url = '/'