Pankaj
Pankaj

Reputation: 10105

Issue while redirect from one page to another

I am using DJango 2.2.6

Profile App

app_name = 'myprofileapp'

urlpatterns = [
    path('profile', accountController.as_view(), name='account')
]

Auth App

app_name = 'authapp'

urlpatterns = [
    path('login', loginController.as_view(), name='login')
]

Below is the code to login user. In case logged in successfully then sends to profile page.

class loginController(View):
    def post(self, request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        userobj = authenticate(username = username, password = password)
        if(userobj == None):
            return HttpResponse("Not Found")
        else:
            login(request, userobj)
            return redirect('profile')          

After this code login(request, userobj) if I write return render(request, 'profile.html') then the url remains login.

and when I write return redirect('profile'), it says

Reverse for 'profile' not found. 'profile' is not a valid view function or pattern name.

Am I missing anything?

Upvotes: 2

Views: 40

Answers (3)

bhaskarc
bhaskarc

Reputation: 9521

path('profile', accountController.as_view(), name='profile')

changename='account' to name='profile'

Upvotes: 1

Anekan Devadoothan
Anekan Devadoothan

Reputation: 77

You must add the app_name while redirecting. Please change the redirect parameter like this

return redirect('myprofileapp:account') 

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

You need to mention the app_name as well, and since you named the view 'account', use that name:

class loginController(View):
    def post(self, request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        userobj = authenticate(username = username, password = password)
        if(userobj == None):
            return HttpResponse("Not Found")
        else:
            login(request, userobj)
            return redirect('myprofileapp:account')

Upvotes: 1

Related Questions