Search code examples
djangodjango-viewsdjango-allauthdjango-autocomplete-light

Django - NoReverseMatch at /<url>/ after login


I'm using django-allauth for user authentication. After login the user must be redirected to /hello/prehome/.'hello' is the name of my app and the view for prehome redirects the user to homepage view along with a user id. I've specified the mentioned url in settings.py file under LOGIN_REDIRECT_URL. However, when I sign in with a registered username and password, I keep getting the error-

NoReverseMatch at /hello/prehome/
Reverse for 'homepage' with arguments '(None,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['hello/homepage/(?P<user_id>[0-9]+)/$']
...    
/app/hello/views.py in prehome
return HttpResponseRedirect(reverse('homepage',args=(user.id,)))

When I register a new user, the page is redirected normally to homepage but the 'else' section of the homepage view is executed.

Following are snippets from the project-

views.py

def register(request):
    if request.POST:
        form=RegistrationForm(request.POST)
        if form.is_valid():
            username=form.cleaned_data['username']
            password=form.cleaned_data['password']
            email=form.cleaned_data['email']
            user=User.objects.create_user(username=username,password=password,email=email)
            user.save()
            user=auth.authenticate(username=username,password=password)
            if user.is_active:
                auth.login(request,user)
                return HttpResponseRedirect(reverse('homepage',args=(request.user.id,)))
    else:
        form=RegistrationForm()
    return render(request,'hello/registration/register.html',{'form':form,})

def prehome(request):
    user=request.user
    return HttpResponseRedirect(reverse('homepage',args=(user.id,)))

def homepage(request,user_id):
    user=User.objects.get(id=user_id)
    if request.user.is_authenticated():
        if request.user.is_active:
            return render(request,'hello/userprofile/homepage.html',{'user':user,})
    else:
        return render(request,'hello/userprofile/error.html')

urls.py (in app's subdirectory)

from django.conf.urls import include, url
from . import views
urlpatterns=[
url(r'^prehome/$',views.prehome,name='prehome'),
url(r'^homepage/(?P<user_id>[0-9]+)/$',views.homepage,name='homepage'),
]

urls.py(in project folder)

from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from hello import views

urlpatterns = [
    url(r'^$',views.index,name='index'),
    url(r'^admin/',include(admin.site.urls)),
    url(r'^accounts/',include('allauth.urls')),
    url(r'^hello/',include('hello.urls')),
]

Please help.


Solution

  • It was probably a syntactical error.
    I changed the return statement from
    return HttpResponseRedirect(reverse('homepage',args=(request.user.id,))) to
    return HttpResponseRedirect(reverse('homepage',args=[request.user.id]))
    and it worked.However, I do not understand where did it go wrong and why did it work after incorporating this change. The official django documentation uses the previous format of passing a parameter to a function.