我尝试了很多不同的操作,但是一旦保存表单,我就无法执行重定向操作。
我的loan_form.html:
<form action="{% url loans %}" method="post">
{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Submit loan request" /></p>
</form>
还有我的urls.py
from django.urls import path
from .views import LoanListView, LoanCreateView
urlpatterns =[
path('', LoanListView.as_view(), name='loans'),
path('create', LoanCreateView.as_view(), name='create')
]
答案 0 :(得分:1)
鉴于LoanListView
是ListView
,而LoanCreateView
是CreateView
,则应发布到LoanCreateView
,因为这将解析表格,并采取正确的措施,因此您应该输入:
<form action="{% url 'create' %}" method="post">
...
</form>
在LoanCreateView
中,您可以通过指定success_url
属性来指定要在处理成功的情况下重定向到的视图。如果您想指定视图的名称,通常reverse_lazy
[Django-doc]可以帮助您:
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
class LoanCreateView(CreateView):
# ...
success_url = reverse_lazy('loans')