Abhijeet Khangarot
Abhijeet Khangarot

Reputation: 1567

**kwargs always giving none in django

Somewhere in my views i am redirecting as :

return redirect('camera_login', success='False' )

In my urls.py :

    url(r'^', CameraLoginView.as_view(), 
        name = "camera_login"),
    url(r'^(?P<success>[a-zA-Z0-9-]+)/$', CameraLoginView.as_view(), 
        name = "camera_login"),

Now i can see the url is being redirected to localhost:8000/False in my browser, and the camera_login.html page is rendered, but when i debug the code, my **kwargs is always none.

class CameraLoginView(View):

def get(self, request, *args, **kwargs):
    print('kwargs' , self.kwargs)
    success = None
    print('args ', args)
    return render( request,
        'camera_login.html', {'success': success})

in terminal kwargs {}, kwargs is always empty? I don't know where i am going wrong. Thanks.

Upvotes: 1

Views: 896

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599630

Your first pattern matches everything, so your second pattern is never reached. Since the first pattern does not have a capturing group, no kwargs are ever captured.

You should make sure you always terminate your patterns:

url(r'^$', CameraLoginView.as_view(), name = "camera_login"),
url(r'^(?P<success>[a-zA-Z0-9-]+)/$', ...)

Upvotes: 3

Related Questions