0

How do I get a URL keyword argument with get() or another method of CreateView? I tried to use self.kwargs['arg_name'], but without result. Thank you in advance.

kalabalik
  • 3,792
  • 2
  • 21
  • 50
O.Vykhor
  • 461
  • 1
  • 5
  • 18

2 Answers2

0

You can access url keywords in view by using following code

Method one

To access the url parameters in class based views, use self.args or self.kwargs so you would access it by doing self.kwargs['user_id']

url(r'^users/(?P<user_id>[0-9]+)/$',
views.your_view_name.as_view()),

In youe views.py:

class your_class_name(CreateView):
    def view_name(self):
        upk=self.kwargs.get['user_id']
        print user_id
        ...

Method 2

If you are passing keyword in url like this

/your_url?user_id=1

views.py

def view_name(request):
    user_id = request.GET.get('user_id')
    print user_id
    ....

Reference from Stackoverflow Link

hope this helps you

NIKHIL RANE
  • 4,012
  • 2
  • 22
  • 45
  • This solution isn't fit to working with class vased views. I'm using CreateView to create objects, and i need to get url key word arguments precisely within CreateView – O.Vykhor Sep 25 '17 at 13:07
  • Again not that. By your example we get kwargs within ListView (in ListView I getting kwargs succesfully), but I need to repeat this procedure pewcisely within CreateView – O.Vykhor Sep 25 '17 at 13:18
0

Do you mean this ?

class CreateMessageView(LoginRequiredMixin, CreateView):

    def get(self, request, *args, **kwargs):
        ret = super(CreateMessageView, self).get(request, *args, **kwargs)
        upk=self.kwargs['user_id']
        return ret

I have tested it... working ... NIKHILS RANEs answer is somewhat correct. this line upk=self.kwargs['user_id'] differs from his upk=self.kwargs.get['user_id']. Maybe thats your problem

mbieren
  • 1,034
  • 8
  • 31
  • No, I tried exactly `self.kwargs['some']`, and your example returne `super(CreateMessageView, self).get(request, *args, **kwargs)`, but how we can use upk in this case? – O.Vykhor Sep 25 '17 at 13:47
  • it is `self.kwargs['pk']` in your case. If you wanna use `'some'` or `'upk'`you will have to specify `r'^lot/create/(?P[0-9]+)/$'` or `r'^lot/create/(?P[0-9]+)/$'` it it does not help I do not understand your problem. If you wanna use upk somewhere else in your view use `self.upk=self.kwargs['upk']` – mbieren Sep 25 '17 at 13:50
  • Yes, I use `self.kwargs['pk']`, and I already tried it within get() and get_context_data() methods of CreateView, but I get the same result, `self.kwargs['pk']` nothing returns. – O.Vykhor Sep 25 '17 at 13:55
  • And within url pattern I'm using `pk` variable name. – O.Vykhor Sep 25 '17 at 13:56
  • I am sure that I am right. Please start a debbuger and examine the variables inside your get method. You have a silly fault here. – mbieren Sep 25 '17 at 13:56
  • I try to check by exist pk variable in `self.kwargs` by `if 'pk' in self.kwargs:` and body of if statement not running – O.Vykhor Sep 25 '17 at 14:08
  • please check the key, value pairs of the dict like this : `for k, v in self.kwargs.items(): print(k, v)` – mbieren Sep 25 '17 at 14:13