Reputation: 39
After loging in correctly, I want to redirect the user to another page so I use redirect, but I get a NoReverseMatch error.
Reverse for 'watchfilms' with arguments '()' and keyword arguments '{}' not found.
I don't know what's going wrong so I'm showing you my urls.py and views.py files. Maybe you see something wrong...
project's url.py
urlpatterns = patterns('',
url(r'^films/', include('films.urls')),
url(r'^admin/', include(admin.site.urls)),
)
app's url.py
urlpatterns = patterns('',
url(r'^$', 'films.views.index'),
url(r'^signup$', 'films.views.signUp'),
url(r'^login$', 'films.views.logIn'),
url(r'^signedupok$', 'films.views.signedUpOk'),
url(r'^watchfilms$', 'films.views.watchFilms'),
url(r'^logout$', 'films.views.logout'),
)
views.py
...
def logIn(request):
error = ""
username = request.POST.get('username', False)
pass = request.POST.get('password', False)
user = authenticate(username = username, password = pass)
if username or pass:
if user is not None:
if user.is_active:
login(request, user)
return redirect('watchfilms')
else:
error = 'Error while login!'
else:
errorea = 'Data is not correct!'
return render(request, 'films/login.html', {'error': error})
@login_required
def watchFilms(request):
film_list = Film.objects.all()
paginator = Paginator(film_list, 3)
page = request.GET.get('page')
try:
films = paginator.page(page)
except PageNotAnInteger:
films = paginator.page(1)
except EmptyPage:
films = paginator.page(paginator.num_pages)
return render(request, 'films/watchFilms.html', {'films': films})
...
Once login is OK, watchFilms view should be called but it looks like it cannot be found (I don't get it why, I think urls.py is OK)...
Upvotes: 1
Views: 930
Reputation: 308849
You should name your url pattern if you want to reverse it:
url(r'^watchfilms$', 'films.views.watchFilms', name='watchfilms'),
Make sure the capitalisation is the same as in your redirect()
call (i.e. all lowercase).
Upvotes: 2
Reputation: 476614
The redirect
function takes as input a model, a view, or an absolute or relative url.
Django is however complaining that it can not find any URL for the given parameter.
Probably the best thing is to pass a reference to the view function, such that if you later change the url, it will still work:
def logIn(request):
error = ""
username = request.POST.get('username', False)
passw = request.POST.get('password', False)
user = authenticate(username = username, password=passw)
if username or passw:
if user is not None:
if user.is_active:
login(request, user)
return redirect(watchFilms)
else:
error = 'Error while login!'
else:
errorea = 'Data is not correct!'
return render(request, 'films/login.html', {'error': error})
Please do not use a variable named pass
. pass
is a Python keyword, and usually the interpreter will error on this.
Upvotes: 0