django在CreateView之后重定向到上一个url

时间:2015-11-07 17:23:51

标签: django django-forms

我想返回提交后调用CreateView,UpdateView和DeleteView的DetailView(BuildingUnitDetail)的url。例如:http://127.0.0.1:8000/unit/13/

我发现了有关重定向到之前视图的其他几个问题/答案,但我无法让任何解决方案适合我。主要是因为我不理解他们。它似乎应该是一个直接的解决方案而且我正在过度思考它们。

那里有无痛的解决方案吗?

任何帮助都会有很大的帮助,已经超过2天了

#urls.py

from django.conf.urls import url
from . import views
from cdpapp.views import BuildingList, BuildingDetail, BuildingUnitDetail, CreateWorkOrder, EditWorkOrder, DeleteWorkOrder


urlpatterns = [
    url(r'^$', BuildingList.as_view(), name='index'),
    url(r'^building/(?P<pk>\d+)/$', BuildingDetail.as_view(), name='building_detail'),
    url(r'^unit/(?P<pk>\d+)/$', BuildingUnitDetail.as_view(), name='building_unit_detail'),
    url(r'^workorder/add/$', CreateWorkOrder.as_view(), name='workorder_add'),
    url(r'^workorder/(?P<pk>\d+)/$', EditWorkOrder.as_view(), name='workorder_update'),
    url(r'^workorder/(?P<pk>\d+)/delete/$', DeleteWorkOrder.as_view(), name='workorder_delete'),
]

#views.py

class BuildingUnitDetail(DetailView):
    model = Unit
    template_name = 'cdpapp/building_units_detail.html'
    context_object_name = 'units'


class CreateWorkOrder(CreateView):
    template_name = 'cdpapp/workorder_form.html'
    model = WorkOrder
    success_url = reverse_lazy('back to calling url')


class EditWorkOrder(UpdateView):
    template_name = 'cdpapp/workorder_form.html'
    model = WorkOrder
    success_url = reverse_lazy('back to calling url')


class DeleteWorkOrder(DeleteView):
    template_name = 'cdpapp/workorder_form.html'
    model = WorkOrder
    success_url = reverse_lazy('back to calling url')

#forms.py

class WorkOrderForm(forms.Form):
    building = forms.ModelChoiceField(queryset=Building.objects.all())
    unit = forms.ModelChoiceField(queryset=Unit.objects.all())
    ...

#form template

{% block content %}
<form method="POST"> {% csrf_token %}
    {{ form.as_p }}
    <input class="btn btn-danger" type="submit" value="Submit">
</form>
{% endblock content %}

1 个答案:

答案 0 :(得分:3)

使用下一个参数可能是一个优雅的解决方案。

这是一个例子(我刚刚编写的未经测试的代码)。基本上使用get_form_kwargs方法确保您的按钮参数被推送到表单在GET上的初始字典。 {POST}数据中可用的form_valid方法扩展名确保success_url属性重载

这样,您仍然可以使用success_url属性以普通方式定义默认值。

注意:您无法信任用户的输入。为简单起见,我只是将CharField用于下一个字段。在现实生活中,您应该检查来自该字段的数据并进行验证。

BuildingUnitDetail模板

<a href="{% url 'workorder_add' %}?next={% url 'building_unit_detail' object.pk %}">
    Add workorder
</a>  # assuming 'object' (Unit) is available in your template's context

WorkOrderForm modelform

class WorkOrderForm(forms.ModelForm):
    next = forms.CharField(required=False)

    class Meta:
        model = WorkOrder
        exclude = tuple()

CreateWorkOrder视图

class CreateWorkOrder(CreateView):
    template_name = 'cdpapp/workorder_form.html'
    form_class = WorkOrderForm

    def get_form_kwargs(self, **kwargs):
        kwargs = super(CreateWorkOrder, self).get_form_kwargs()
        redirect = self.request.GET.get('next')
        if redirect:
            if 'initial' in kwargs.keys():
                kwargs['initial'].update({'next': redirect})
            else:
                kwargs['initial'] = {'next': redirect}
        return kwargs

    def form_invalid(self, form):
        import pdb;pdb.set_trace()  # debug example
        # inspect the errors by typing the variable form.errors
        # in your command line debugger. See the pdb package for
        # more useful keystrokes
        return super(CreateWorkOrder, self).form_invalid(form)

    def form_valid(self, form):
        redirect = form.cleaned_data.get('next')
        if redirect:
            self.success_url = redirect
        return super(CreateWorkOrder, self).form_valid(form)

第二个想法?

避免重定向,您还可以在弹出窗口中处理这些“任务”或在单个视图中处理多个表单。这会增加复杂性,但后者可能可以增强用户体验。