[Solved]-Django reverse using kwargs

19👍

Your url definition is wrong, also sounds like your misunderstood what kwargs does in reverse. It feeds the named arguments of your url definition with it’s value, not passing context to the view.

For example, you could do this:

url(r'test_suite/(?P<test_name>\w+)/(?P<test_id>\d+)$', 'test', name='test')
reverse('test', kwargs={'test_name': 'haha', 'test_id': 1})

kwargs will substitute your test_name in url definition with haha and test_id with 1. This will produce a url: test_suite/haha/1.

2👍

Kwargs in reverse are the variables that go into your URL to make it unique. It would be really odd to see a message/date inside a URL…which is what your URLconf is trying to do.

Also, you can’t redirect with context in Django without going to trouble that makes the juice not worth the squeeze. One option is to use the messages framework to display a message on the login page if you want to do that after the redirect.

My solution would be to remove the kwargs from your URLConf for /login/ and use the messages framework (or sessions) to pass the information to the login page after the redirect.

Leave a comment