后重定向到上一个

后重定向到上一个

本文介绍了django 在 CreateView 后重定向到上一个 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to return to the url of a DetailView(BuildingUnitDetail) that called the CreateView, UpdateView, and DeleteView after a Submit. ex: http://127.0.0.1:8000/unit/13/

我发现了其他几个关于重定向到先前视图的问题/答案,但我找不到任何适合我的解决方案.主要是因为我不明白他们.似乎这应该是一个直接的解决方案,但我想多了.

I've found several other questions/answers regarding redirects to previous view but I couldn't get any of the solutions to work for me. Mostly because I don't understand them. It seems that it should be a straight forward solution and I'm overthinking them.

有没有无痛的解决方案?

Is there a painless solution out there?

任何帮助都会非常感谢,在这方面已经超过 2 天了

Any help would be greatly appeciated, been at this for over 2 days

#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())
    ...

#表单模板

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

推荐答案

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

Using a next parameter could be an elegant solution.

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

Here's an example (untested code I just wrote this). Basically using the get_form_kwargs method makes sure your button parameter is pushed to the form's initial dict on GET. The form_valid method extension makes sure the success_url property is overloaded when available in POST data

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

This way you can still define a default using the success_url property in an ordinary fashion.

注意:您不能相信用户的输入.为简单起见,我只是在下一个字段中使用了 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 模型表单

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)


第二个想法?

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

Avoiding redirects, you could also handle these 'tasks' in a popup or handle multiple forms in a single view. This would increase complexity, but the latter might enhance user experience.

这篇关于django 在 CreateView 后重定向到上一个 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:42