如何使用变量reverse_lazy到view / url?

时间:2017-09-12 19:41:36

标签: python django django-views django-urls

我有一个DeleteView用于从模型中删除对象。它通过表格中的按钮启动。它工作正常。

class DeleteAssignment(DeleteView):
  model = Assignment
  success_url = reverse_lazy('company')

Screenshot

我只想让它回到成功的父视图。我目前已将其重定向到父级的父级(company),因为此视图不需要变量。这很简单,但父视图需要一个变量farm_id来渲染,这是从URL中捕获的,如“/ farm / 18”

url(r'^farm/(?P<farm_id>.+)', views.farm, name='farm'),

我已通过页面上的表单解决了这个问题,方法是将其重定向到带有变量farm的视图farm_id

return redirect(farm, farm_id=farm_id)

我如何使用success_url的{​​{1}}来完成此操作?

尼基的例子:

views.py

DeleteView

urls.py

 @login_required
    def farm(request, farm_id=None):
        user = request.user
        company_id = user.profile.company_id
        farm_id = farm_id
        farm_filter = Farm.objects.filter(farm=farm_id)
        farm_current = farm_filter[0]
        # farm_company =
        serial_filter = Sensor.objects.filter(assignment__farm__company_id__isnull=True)
        assignment_filter = Assignment.objects.filter(farm=farm_id)
        farm_instance = Farm.objects.get(farm=farm_filter[0].farm)
        update_farm_form = UpdateFarmForm(instance=farm_instance)
        assign_sensor_form = AssignmentForm()

        if request.method == 'POST' and 'updatefarm' in request.POST:
            update_farm_form = UpdateFarmForm(request.POST, instance=farm_instance)
            if update_farm_form.is_valid():
                update_farm_form.save()
                return redirect(farm, farm_id=farm_id)
        else:
            if request.method == "POST" and 'assignsensor' in request.POST:
                assign_sensor_form = AssignmentForm(request.POST)
                if assign_sensor_form.is_valid():
                    assignment = assign_sensor_form.save()
                    if user.is_staff is True:
                        assignment.company_id = farm_filter[0].company_id
                    else:
                        assignment.company_id = user.profile.company_id
                    assignment.save()
                    return redirect(farm, farm_id=farm_id)
                else:
                    assign_sensor_form = AssignmentForm(request.POST)
        return render(request, 'users/farm_template.html', {'company_id': company_id, 'farm_filter': farm_filter, 'farm_current': farm_current, 'serial_filter': serial_filter, 'assignment_filter': assignment_filter, 'update_farm_form': update_farm_form, 'assign_sensor_form': assign_sensor_form})

这是视图在成功提交表单后重定向到自身。这会根据用户提交的内容重新加载表,而不必立即处理js。

from django.conf.urls import url, include from django.contrib.auth import views as auth_views from apps.dashboard import views from apps.dashboard.views import DeleteAssignment, DeleteFarm urlpatterns = [ url(r'^company/', views.company_view, name='company'), # url(r'^test/', views.test), # url(r'^test2/', views.test2, name='test2'), # url(r'^farms/', views.add_farms), url(r'^manual-signup/', views.signup_manual), url(r'^signup/(?P<company_id>.+)', views.signup_company), url(r'^settings/', views.update_profile), url(r'^create-company/', views.create_company), url(r'^create-sensor/', views.create_sensor), url(r'^assign-sensor/', views.assign_sensor), url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'), url(r'^logout/$', auth_views.logout, {'next_page': '/login'}, name='logout'), url(r'^$', views.main), # url(r'^company/(?P<company_id>.+)/farm/(?P<farm_id>.+)', views.farm), url(r'^farm/(?P<farm_id>.+)', views.farm, name='farm'), url(r'^unit/(?P<sensor_id>.+)', views.unit), # url(r'^company/(?P<company_id>.+)', views.company), url(r'^download/', views.download), # url(r'^export/xls/$', views.export_users_xls), url(r'^live/', views.live), url(r'^data/', views.data), url(r'^debug/', views.debug), url(r'^delete-assignment/(?P<pk>\d+)/$', DeleteAssignment.as_view(), name='delete-assignment', ), url(r'^delete-farm/(?P<pk>\d+)/$', DeleteFarm.as_view(), name='delete-farm', ), url(r'^', include('django.contrib.auth.urls')) ] 中的farm_id是通过 urls.py return redirect(farm, farm_id=farm_id)

中的相应行从网址捕获的

这就是我想要删除视图的所有内容,而不是成功网址只是一个视图,一个像我上面那样的变量视图。

2 个答案:

答案 0 :(得分:4)

,您可以通过以下方式使用success_url执行此操作:

class DeleteAssignment(DeleteView):
    . . . . . 
    . . . . . 

    . . . . . 
    def get_success_url(self):
          # if you are passing 'pk' from 'urls' to 'DeleteView' for company
          # capture that 'pk' as companyid and pass it to 'reverse_lazy()' function
          companyid=self.kwargs['pk']
          return reverse_lazy('company', kwargs={'pk': companyid})

这应该完美无缺。

答案 1 :(得分:0)

正如Nickie在评论中所说:https://docs.djangoproject.com/en/1.11/ref/class-based-views/mixins-editing/#django.views.generic.edit.DeletionMixin.success_url

使用success_url = "/farm/{farm_id}"

解决了这个问题