Home > Net >  django - i want to redirect to the profile page after a user edits thier profile
django - i want to redirect to the profile page after a user edits thier profile

Time:01-04

when a user finish updating thier profile i want to redirect them back to the profile page but it keeps showing that Reverse for 'profile' with no arguments not found. 1 pattern(s) tried: ['(?P<username>[^/] )/$'] i have tried making my views redirect but it now.

views.py

def profile_update(request):
    Profile.objects.get_or_create(user=request.user)
    if request.method == "POST":
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'Acount Updated Successfully!')
            return redirect('profile')
    else:
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
    
    context = {
        'u_form': u_form,
        'p_form': p_form,
        
    }

    return render(request, 'userauths/profile_update.html', context)

urls.py main project dir

from userauths.views import UserProfile

urlpatterns = [
    path('admin/', admin.site.urls),
    path('users/', include('userauths.urls')),
    path('<username>/', UserProfile, name='profile'),
]

CodePudding user response:

from django.contrib.auth.decorators import login_required

try :

@login_required
def profile_update(request):
    profile = get_object_or_404(Profile, user=request.user)


return redirect('profile', profile.user.username)
     
  •  Tags:  
  • Related