简体   繁体   中英

Django URL redirecting issue

i have a redirect url problem when the user complete editing his profile informations ,i want to redirect to the profile page , but it display to me 404 error this is My view.py file :

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)

thi is my url.py file :

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'))    
]

The Error i got :

Request Method:     POST
Request URL:    http://127.0.0.1:8000/profile_update/
.
.
.
The current path, profile_update/, didn't match any of these. 

The problem is that your url expects an id with the url (ie localhost:8000/profile_update/12 ), but when you are making a post request, you are not sending one.

So I am guessing you need to update the code like this:

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)

And update the template as well:

<form name="form" method="post" action="{% url 'profile-update' pk %}">

Try changing the redirect line from

return redirect(reverse('profile-detail'))

to

return redirect('app-name:profile-detail')

where app-name is the name of your django app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM