1👍
Please, read again the doc of django and the reverse method:
in your views.py
def index(request):
return render(request, "app/index.html")
def user(request):
return render(request, "app/user.html")
def userLogin(request):
loginUser = authenticate(username = request.POST["username"], password = request.POST["password"])
if loginUser is not None:
if loginUser.is_active:
login(request, loginUser)
return HttpResponseRedirect(reverse("user")) #See that! it's a url name!
return HttpResponse(reverse("index")) # What happens if can't log in?
in your urls.py
urlpatterns = patterns("",
url(r'^$', views.index, name = "index"),
url(r'^user/$', views.user, name="user"), #Note the "user"!
url(r'^registerNewUser/$', views.registerNewUser),
url(r'^userLogin/$', views.userLogin),
)
0👍
Here’s the solution that worked for me. It consisted of using reverse()
with HttpResponse()
.
Specifically,
def index(request):
return render(request, "app/index.html")
def user(request):
if request.user.is_authenticated:
if not request.user.is_staff:
return render(request, "app/user.html")
else:
return render(request, "app/admin.html")
else:
return HttpResponse(reverse(index))
def userLogin(request):
loginUser = authenticate(username = request.POST["username"], password = request.POST["password"])
if loginUser is not None:
if not loginUser.is_active:
loginUser.is_active = True
loginUser.save()
login(request, loginUser)
#Note HttpResponse and unquoted user method
return HttpResponse(reverse(user))
else:
return HttpResponse(simplejson.dumps({"login" : "failed"}))
combined with the following urls.py
configuration
urlpatterns = patterns("",
url(r'^$', views.index, name = "index"),
url(r'^$', views.user, name = "user"),
url(r'^user/$', views.user, name = "user"),
url(r'^registerNewUser/$', views.registerNewUser),
url(r'^userLogin/$', views.userLogin),
)
got the redirect working to the relevant pages after a successful login or registration.
- [Answer]-Referencing relative directories outside of project
- [Answer]-Best-practice: django + python analytics
- [Answer]-URL Conf – Serving views at the root URL and non root URLs within one app
- [Answer]-Use HttpResponse with JSON data in this code
- [Answer]-DRF 1.7.1 'str' object has no attribute 'resolve' AttributeError
Source:stackexchange.com