我正在尝试在Django应用程序中编写“保存并添加另一个”功能。
我在按钮上添加了elif条件。我在网络上找不到任何有用的资源和参考,尤其是我的代码集。我试过将两个return放在elif内,并将self.object片段内。有什么建议我可以做什么?我对Django非常陌生。请帮忙。
Views.py
def post(self, request, *args, **kwargs):
if "cancel" in request.POST:
return HttpResponseRedirect(reverse('skillsMatrixApp:index'))
elif "another" in request.POST:
#try 1
#self.object = self.get_object()
#url = self.get_success_url()
#return HttpResponseRedirect(url)
#try 2
#return HttpResponseRedirect(reverse('skillsMatrixApp:employee-add'))
#return super(employeeCreate, self).post(request, *args, **kwargs)
else:
return super(employeeCreate, self).post(request, *args, **kwargs)
employee_form.html
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" formnovalidate name="cancel" class="btn btn-danger">Cancel</button>
<button type="submit" formnovalidate class="btn btn-success">Submit</button>
<button type="submit" formnovalidate name="another" class="btn btn-danger">Save and add another</button>
</div>
urls.py
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'employee/$', views.employeeList.as_view(), name='employee-list'),
url(r'employee/add/$', views.employeeCreate.as_view(), name='employee-add'),
答案 0 :(得分:1)
我假设您对在保存和添加新内容中做两件事感到困惑。所以,我将从你写的内容开始:
我认为您应该在views.py
def post(self, request, *args, **kwargs):
if "cancel" in request.POST:
return HttpResponseRedirect(reverse('skillsMatrixApp:index'))
elif "another" in request.POST:
# You can call the super to invoke the functionality without
# returning the result.
super(employeeCreate, self).post(request, *args, **kwargs)
return HttpResponseRedirect(reverse('skillsMatrixApp:employee-add'))
else:
return super(employeeCreate, self).post(request, *args, **kwargs)
以及遵循DRY原理的更好版本。您可以这样做:感谢@bruno评论
def post(self, request, *args, **kwargs):
save_action = None
if "cancel" in request.POST:
return HttpResponseRedirect(reverse('skillsMatrixApp:index'))
else:
save_action = super(employeeCreate, self).post(request, *args, **kwargs)
if "another" in request.POST:
return HttpResponseRedirect(reverse('skillsMatrixApp:employee-add'))
return save_action
答案 1 :(得分:0)
我更愿意为您创建两个通用类,即CreateView和RedirectView。 CreateView具有保存对象(帖子)并返回默认页面的方法,而RedirectView具有设置取消URL的方法。接下来以html形式,您可以设置一些形式并将操作添加到按钮。例如
<form action="{% url 'create_view' %}" method="post">
{% for field in form %}
{{ field.label_tag }}
{% if field.help_text %}
<sup>{{ field.help_text }}</sup>
{% endif %}
{{ field.errors }}
{{ field }}
{% endfor %}
<button type="submit">Save and add another</button><br>
<button type="submit" formaction="{% url 'redirect_url' %}">Cancle</button>
</form>
答案 2 :(得分:0)
“保存并添加另一个”功能只是重定向到同一页面,而不是重定向到默认的success_url
。您可以在post()
方法本身中完成此操作,即:
def post(self, request, *args, **kwargs):
if "cancel" in request.POST:
return HttpResponseRedirect(reverse('skillsMatrixApp:index'))
next = super(employeeCreate, self).post(request, *args, **kwargs)
if "another" in request.POST:
next = HttpResponseRedirect(reverse('skillsMatrixApp:employee-add'))
return next
但是,如果表单未通过验证,则此操作将无法按预期工作(在这种情况下,应重新呈现带有错误的表单)。更好的解决方案是覆盖get_success_url()
-仅在发布成功后才会调用-并在此处检查self.request.POST
:
class YourView(...):
def post(self, ...):
if "cancel" in request.POST:
return HttpResponseRedirect(reverse('skillsMatrixApp:index'))
return super(...).post(...)
def get_success_url(self):
if "another" in self.request.POST:
return reverse('skillsMatrixApp:employee-add')
# else return the default `success_url`
return super(...).get_success_url()