I am writing a view for editing a title in django (at 'project_edit.html'
, and redirecting to the 'project_detail.html'
page, but (loosely) following the tutorial here I'm using the redirect method:
我正在编写一个用于在django中编辑标题的视图(在'project_edit.html',并重定向到'project_detail.html'页面,但是(松散地)遵循这里的教程我正在使用重定向方法:
def project_edit(request, project_id):
project = get_object_or_404(Project, pk=project_id)
if request.method == "POST":
form = ProjectForm(request.POST)
if form.is_valid():
project_update = form.save(commit=False)
project.title = project_update.title
project.save()
return redirect(to='gantt_charts.views.project_detail', kwargs={'pk', project.pk}, permanent=True)
else:
messages.error(request, "Form invalid!")
return render(request, 'project_edit.html', {'project':project, 'form':form})
else:
form = ProjectForm()
return render(request, 'project_edit.html', {'project':project, 'form':form})
In the tutorial, they also use redirect:
在本教程中,他们还使用重定向:
def post_new(request):
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('blog.views.post_detail', pk=post.pk)
else:
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})
But when I click my link http://localhost:8000/project/2/edit
and submit my form, I get sent to: http://localhost:8000/project/2/gantt_charts.views.project_detail
但当我点击我的链接http:// localhost:8000 / project / 2 /编辑并提交我的表单时,我会收到:http:// localhost:8000 / project / 2 / gantt_charts.views.project_detail
I cannot fathom why.
我无法理解为什么。
Looking at the redirect
definition in shortcuts.py and then the resolve_url
definition, my only guess is that it drops to the bottom and it just returned as is. Why isn't the lookup performed?
查看shortcuts.py中的重定向定义,然后查看resolve_url定义,我唯一的猜测就是它会降到最底层,它只是按原样返回。为什么不执行查找?
ed:
ED:
Here are my url patterns for that app:
以下是该应用的网址模式:
urlpatterns = patterns('',
url(r'^(?:project)?/?$', views.project_list),
url(r'^project/(?P<project_id>\d+)/edit$', views.project_edit),
url(r'^project/(?P<project_id>\d+)/[A-z\\-]{0,50}$', views.project_detail),
)
1 个解决方案
#1
1
Add a name to your project detail url:
在项目详细信息URL中添加名称:
url(r'^project/(?P<project_id>\d+)/[A-z\\-]{0,50}$',
views.project_detail, name='project_detail`),
And then refer to this name in redirect()
call:
然后在redirect()调用中引用此名称:
return redirect('project_detail', project_id=project.pk, permanent=True)
#1
1
Add a name to your project detail url:
在项目详细信息URL中添加名称:
url(r'^project/(?P<project_id>\d+)/[A-z\\-]{0,50}$',
views.project_detail, name='project_detail`),
And then refer to this name in redirect()
call:
然后在redirect()调用中引用此名称:
return redirect('project_detail', project_id=project.pk, permanent=True)