Home > Blockchain >  Getting NoReverseMatch error when trying to redirect user after form upload
Getting NoReverseMatch error when trying to redirect user after form upload

Time:01-06

I am getting the error NoReverseMatch in Django when I try to redirect the user to a new URL after processing a form upload and successfully saving the form. Here is my views.py

@login_required
def profile(request):
    if request.method == 'POST':
        profile_form = UpdateProfileForm(request.POST, request.FILES, instance=request.user.profile)
        files = request.FILES.getlist('resume')
        resumes_data = []
        if profile_form.is_valid():
            for file in files:
                try:
                    # saving the file
                    resume = profile_form.cleaned_data['resume']

                    parser = ResumeParser(file.temporary_file_path())
                    data = parser.get_extracted_data()
                    resumes_data.append(data)
                    profile_form.instance.name = data.get('name')
                    profile_form.instance.email              = data.get('email')
                    profile_form.instance.mobile_number      = data.get('mobile_number')
                    if data.get('degree') is not None:
                        profile_form.instance.education      = ', '.join(data.get('degree'))
                    else:
                        profile_form.instance.education      = None
                        profile_form.instance.company_names      = data.get('company_names')
                        profile_form.instance.college_name       = data.get('college_name')
                        profile_form.instance.designation        = data.get('designation')
                        profile_form.instance.total_experience   = data.get('total_experience')
                    if data.get('skills') is not None:
                        profile_form.instance.skills         = ', '.join(data.get('skills'))
                    else:
                        profile_form.instance.skills         = None

                    if data.get('experience') is not None:
                        profile_form.instance.experience     = ', '.join(data.get('experience'))
                    else:
                        profile_form.instance.experience     = None
                    profile_form.save()
                    return redirect('users-profile')
                except IntegrityError:
                    messages.warning(request, 'Duplicate resume found')
                    return redirect('users-profile')
                
        profile_form.save()
        messages.success(request, 'Your profile is updated successfully')
        # return redirect(reverse('userprofile', kwargs={"id": request.user}))
        return redirect('userprofile')
    else:
        profile_form = UpdateProfileForm(instance=request.user.profile)

    return render(request, 'user/resumeprofile.html', {'profile_form': profile_form})

@login_required
def myprofile(request, user_id):
    profile = Profile.objects.get(id=user_id)
    context = {'profile':profile}
    return render(request, 'user/profile.html', context)

And here is my urls.py:

urlpatterns = [
    path('', jobs_views.home, name='home'),   #this is home function inside the views.py in the jobs folder
    path('contact',jobs_views.contact, name='contact'),
    # path('jobs/', jobs_views.job_list, name='job-list'),
    path('jobs/<slug:slug>', jobs_views.job_detail, name='job_detail'), #slug will help us identify the specific instance of job

    path('jobs-search/', jobs_views.job_search, name='job_search'),
    path('profile/', jobs_views.profile, name='users-profile'),
    path('userprofile/<int:user_id>',jobs_views.myprofile, name='userprofile')

]

The error occurs when I redirect the user to userprofile but I don't get any error when I redirect them to users-profile. Attached is a screenshot of the error message displayed. The Screenshot of the error

Any help will be highly appreciated.

CodePudding user response:

You can go around it by just taking the user id from the request since they are required to be logged in.

In your urls.py paths, change the path userprofile to :

path('userprofile/',jobs_views.myprofile, name='userprofile') # remove id parameter

Then in your myprofile view, change to:

@login_required
    def myprofile(request):
        user_id = request.user.id
        profile = Profile.objects.get(id=user_id)
        context = {'profile':profile}
        return render(request, 'user/profile.html', context)
  • Related