gpt4 book ai didi

CreateView 后 django 重定向到以前的 url

转载 作者:行者123 更新时间:2023-12-02 04:16:12 25 4
gpt4 key购买 nike

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

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

有无痛的解决方案吗?

任何帮助将不胜感激,已在此工作超过 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())
...

#表单模板

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

最佳答案

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

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

这样您仍然可以以普通方式使用 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 模型形式

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

class Meta:
model = WorkOrder
exclude = tuple()

CreateWorkOrder View

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)


再想一想?

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

关于CreateView 后 django 重定向到以前的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33585579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com