python - Redirecting Django built-in login View to a URL taking user's pk as argument -
i trying redirect url taking user's pk argument after successful log-in using django's built-in login view.
instead of dynamic {{ next }} variable in login.html have generic landing view of logged-in users;
<input type="submit" value="login" /> <input type="hidden" name="next" value="{% url 'userredirect' %}" />
in urls.py have;
url(r'^users/', views.users, name='userredirect'), url(r'^(?p<pk>\d+)/', userhome.as_view(), name='userhome'),
and in views.py have
@login_required def users(request): url = reverse('userhome', kwargs={'pk':request.user.id}) return httpresponseredirect(url)
what doing here redirect detail view have named userhome
on user model after successful login using 2 redirects not know of way redirect userhome
directly (it takes user's pk argument). works , indeed redirected user's homepage when checking via browser.
reference;
the "next" parameter, redirect, django.contrib.auth.login
but when running below test
def test_page_redirects_to_user_home_on_login(self): """ test assure login page redirects user's home page """ username = "someusername" password = "somepassword" user = user.objects.create_user(username=username, password=password) user.save() response = self.client.post(reverse("userlogin"), {"username":username, "password":password}, follow=true) assert response.path == self.client.get(reverse("userhome", kwargs={"pk":user.id} ) )
i below failure
attributeerror: 'httpresponsenotfound' object has no attribute 'path'
it seems test client gets no page. using userredirect
view redirecting , client not go ahead , userhome
class view context.
i'm newbie django/python. please sort out me :).
i forward either way can redirect directly template login
view userhome
or way rewrite test.
hard without more insight in project. here few possibilities , such.
response has no path
response
indeed has no path
, wanted this:
assert response.wsgi_request.path == reverse("userhome", kwargs={"pk":user.id})
include next
in test
you're simulating data login form, you're omitting next
field. add posted data:
{"username":username, "password":password, "next": '/users/',}
take what's in response
it might see what's in response in test. example:
print(response.redirect_chain)
perhaps you're not reaching login page?
are missing login_url
in settings.py
?
login_url = '/login/'
without it, you'll redirected '/accounts/login/', might 404 you're seeing.
finaly - why? :)
perhaps have special use case, i'd read user's id
(a.k.a. pk
) request.user
. way (for example) can't access example.com/<your_id>
, access homepage. of course, might intend. in case i'd still have separate url current user, pay off later. this:
... url(r'^/', userhome.as_view(), name='userhome'), url(r'^(?p<pk>\d+)/', userhome.as_view(), name='userhome'), ...) class userhome(detailview): # protect loginrequiredmixin model = user def get_object(self, queryset=none): if queryset none: queryset = self.get_queryset() id = self.kwargs.get('pk', self.request.user.id) return queryset.filter(id=id).get()