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.
Asked
Active
Viewed 36 times
0
-
may be `self.request.GET.get('arg_name')` ? – Brown Bear Sep 25 '17 at 12:59
-
What is the URL pattern of the view? – Alasdair Sep 25 '17 at 13:30
-
@Alasdair, `url(r'^lot/create/(?P
[0-9]+)/$', views.LotCreate.as_view(), name='lot-create')` – O.Vykhor Sep 25 '17 at 13:33 -
1Then you want `self.kwargs['pk']`. If that doesn't work, then you need to explain what the problem is in more detail -- it's not clear what you mean by 'without result'. – Alasdair Sep 25 '17 at 13:36
-
@Alasdair, self.kwargs['pk'] it's returned None. – O.Vykhor Sep 25 '17 at 13:40
-
What url are you going to in your browser? – Alasdair Sep 25 '17 at 13:52
-
@Alasdair `127.0.0.1:8000/lot/create/2/` – O.Vykhor Sep 25 '17 at 14:09
-
As several people have said, `self.kwargs['pk']` should simply work. You'll need to add more information on how to reproduce the problem. Perhaps a different URL pattern is matching the URL first. – Alasdair Sep 25 '17 at 14:12
2 Answers
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 – mbieren Sep 25 '17 at 13:50[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']` -
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
-
-
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