成功时如何重定向UpdateView? [英] How to redirect an UpdateView upon success?
问题描述
我创建了一个小的Django应用程序来管理适合简单模型的数据.现在,我只需要两个视图:一个视图列出所有记录,另一个视图以通用形式编辑记录.除成功更新后从编辑视图重定向之外,其他所有功能均按预期运行. urls.py
中包含以下内容:
I created a small Django application to manage data that fits a simple a model. For now I only need two views: one to list all records and another to edit a record with a generic form. Everything functions as expected, except the redirection from the edit view upon a successful update. In urls.py
are the following contents:
from django.urls import path
from . import views
app_name = 'reqs'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.ReqUpdateView.as_view(), name='update'),
]
在 forms.py
中:
from django.forms import ModelForm
from .models import Requirement
class RequirementForm(ModelForm):
class Meta:
model = Requirement
fields = ['name', 'priority', 'source' , 'rationale']
还有庙宇 requirement_form.html
:
<h1>{{ requirement.id }} - {{ requirement.name }}</h1>
<form method="post" novalidate>
{% csrf_token %}
<table>
{{ form.as_table }}
<tr><td></td><td><button type="submit">Save</button></td></tr>
</table>
</form>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<br><br>
<a href="{% url 'reqs:index' %}">Back to list</a>
最后一次尝试将更新重定向到列表的 views.py
:
Finally views.py
, on a first attempt to redirect the update to the list:
from django.views.generic import ListView, UpdateView
from django.urls import reverse_lazy
from .models import Requirement
from .forms import RequirementForm
class IndexView(ListView):
template_name = 'reqs/index.html'
context_object_name = 'requirements_list'
def get_queryset(self):
return Requirement.objects.order_by('subject')
class ReqUpdateView(UpdateView):
model = Requirement
form_class = RequirementForm
success_url = reverse_lazy('/')
使用此公式,保存按钮会产生以下错误:
With this formulation the Save button produces this error:
找不到反向的'/'."/"不是有效的视图函数或模式名称.
Reverse for '/' not found. '/' is not a valid view function or pattern name.
我还尝试使用空字符串作为 reverse_lazy
的参数以及路径名 index
,但是会生成类似的错误消息.
I also tried an empty string as argument to reverse_lazy
, as well as the path name index
, but a similar error message is produced.
再次尝试重定向到同一页面,重新定义 get_success_url
方法什么也不做:
On a second attempt I tried to redirect to the same page, redefining the get_success_url
method to do nothing:
class ReqUpdateView(UpdateView):
model = Requirement
form_class = RequirementForm
context_object_name = 'requirement_update'
def get_success_url(self):
pass #return the appropriate success url
这将返回404错误,尝试将浏览器重定向到/reqs/1/None
.
This returns a 404 error trying to redirect the browser to /reqs/1/None
.
第三次尝试重定向到具有相同记录的表单:
A third attempt to redirect to the form with the same record:
class ReqUpdateView(UpdateView):
model = Requirement
form_class = RequirementForm
context_object_name = 'requirement_update'
def get_success_url(self):
pk = self.kwargs["pk"]
return reverse("update", kwargs={"pk": pk})
哪个抱怨找不到视图:
未找到反向的更新".更新"不是有效的视图函数或模式名称.
Reverse for 'update' not found. 'update' is not a valid view function or pattern name.
如何将成功重定向到有效URL?只要有效,它既可以是项目列表,也可以是项目更新视图.
How can I redirect success to a valid URL? It can either be the items list or the item update view, as long as it works.
推荐答案
reverse/reverse_lazy用于使用视图名称或模式名称获取URL.如果您想直接使用网址,只需输入:
reverse / reverse_lazy are used to get the url using view name or pattern name. If you want to use a url directly just write:
success_url = '/'
对于 return reverse("update",kwargs = {" pk:pk})
而言,由于您设置了 app_name ='reqs'
您应该使用 return reverse("reqs:update",kwargs = {" pk:pk})
.
For the case of return reverse("update", kwargs={"pk": pk})
not working since you set app_name = 'reqs'
you should be using return reverse("reqs:update", kwargs={"pk": pk})
instead.
这篇关于成功时如何重定向UpdateView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!