当用户完成编辑个人资料信息时,我遇到了重定向网址问题,我想重定向到个人资料页面,但它向我显示404错误 这是我的view.py文件:
def ProfileView(request, pk=None):
prof = Profile.objects.all()
if pk:
pr = User.objects.get(pk=pk)
else:
pr = request.user
context= {
'pro':prof,
'profile':pr
}
return render(request,'profile.html',context)
def update_profile(request,id):
profile = get_object_or_404(Profile,id=id)
form = ProfileForm(request.POST or None ,request.FILES or None,instance=profile)
if request.method=='POST':
if form.is_valid:
form.save()
return redirect(reverse('profile-detail'))
context = {
'form':form
}
return render(request,'profile_update.html',context)
这是我的url.py文件:
urlpatterns = [
path('admin/', admin.site.urls),
path ('',index),
path ('events_details/<id>',events_details,name="events_details"),
path ('evenements/',evenements,name="events"),
path ('projets/',projets,name="project"),
path ('project_detail/<id>/',project_detail,name="project-detail"),
path ('create_post',create_post,name="create_post"),
path ('project_detail/<id>/update_post',update_post,name="update_post"),
path ('project_detail/<id>/delete_post',delete_post,name="delete_post"),
#------------------------------------------------------------
path ('profile/',ProfileView,name="profile-detail"),
path ('profile_update/<id>',update_profile,name="profile-update"),
path('tinymce/', include('tinymce.urls')),
path('accounts/', include('allauth.urls')),
path('api-auth/', include('rest_framework.urls'))
]
我得到的错误:
Request Method: POST
Request URL: http://127.0.0.1:8000/profile_update/
.
.
.
The current path, profile_update/, didn't match any of these.
答案 0 :(得分:1)
问题在于您的网址要求该网址带有 id (即localhost:8000/profile_update/12
),但是在发出发布请求时,您没有发送请求。
所以我猜您需要像这样更新代码:
def update_profile(request,id):
profile = get_object_or_404(Profile,id=id)
form = ProfileForm(request.POST or None ,request.FILES or None,instance=profile)
if request.method=='POST':
if form.is_valid:
form.save()
return redirect(reverse('profile-detail'))
context = {
'form':form,
'pk': id
}
return render(request,'profile_update.html',context)
并同时更新模板:
<form name="form" method="post" action="{% url 'profile-update' pk %}">
答案 1 :(得分:0)
尝试从
更改重定向行return redirect(reverse('profile-detail'))
到
return redirect('app-name:profile-detail')
其中app-name
是您的Django应用的名称。