我想保存表单中的数据,然后单击“添加”按钮返回上一页。它显示出一些错误。任何人都可以建议正确的方法吗?
template.html
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Add your own Category </h3>
</div>
<div class="panel-body">
<form method="POST" class="post-form">
{% csrf_token %}
{{ form.as_p }}
</div>
</div>
</div>
<input type="hidden" name="next" value="{{ request.path }}">
<button type="submit" class="save btn btn-default" >Add</button>
</form>
views.py
class CustomCategory(LoginRequiredMixin,CreateView):
model = Category
form_class = CategoryForm
def form_valid(self, form):
obj = form.save(commit=False)
def category(request):
next = request.POST.get('next', '/')
return render (request,HttpResponseRedirect(next))
答案 0 :(得分:0)
而不是增加重定向为形式的元件,的为什么不&#39;吨使用视图上的success_url字段,或者如果你需要去URL依赖的对象上,使用在所述get_success_url方法查看?
https://docs.djangoproject.com/en/2.0/ref/class-based-views/mixins-editing/