What is the difference between:
request.post.get('blog','')
AND
request.post.get('blog')
I am not able to figure out what is the difference between these two and what they return.
request.post.get('blog','') will return an empty string if the value is missing in the POST, as you defined it as default in the .get()
request.post.get('blog') will return None if the value is missing in the POST, as you did not defined any default value in .get()
Docs: https://docs.python.org/3/library/stdtypes.html#dict.get
The REQUESR.POST is like a normal dictionary in python, so when you trying to access an element in first example,
You telling python "return the value of BLOG and if it not there just return an empty string", and you can change the default value (the empty str) to whatever you want.
In the second example you didn't provide any default value so if there is no BLOG key in the request it will raise an ERROR, and won't return anything.
Related
usually I use POST or GET requests except for GET.get paginations, but I don't understand the concept there are only two possibilities POST or GET .
example even if there is the same effect I do not understand the difference between
request.GET.get('page') and request.GET["page"] request.POST['rate'] and request.POST.get('rate')
request.POST is a dict-like object.
For dicts and their derivatives, d[x] equates to indexing into the dict by key x, and d.get(x, default) is a method that is equivalent to indexing, except it returns a default value instead of throwing a KeyError. If the default value is not set, d.get() will return None.
request.POST['sth'] will raise a KeyError exception if 'sth' is not in request.POST.
request.POST.get('sth') will return None if 'sth' is not in request.POST
This is from print(request.POST)
<QueryDict: {'csrfmiddlewaretoken': ['2Tg4HgJ07qksb3hPUDWSQYueYOjYOkQcmzll9fnjbJ0GZHkWHdM8DtYqZB4uv3Fv'], 'username': ['boycececil'], 'password': ['password']}>
This is from print(request.POST.get('username'))
boycececil
So as you can see, list (from one of the values of QueryDict)-> string(from the get function), that's magic! Isn't it?
So, somebody know what's going on?
What the value type of the value of request.POST in django?
The type is django.http.request.QueryDict
So as you can see, list -> string, that's magic! Isn't it?
No, it isn't magic. This is just the documented behavior of QueryDict:
"QueryDict.__getitem__(key)
Returns the value for the given key. If the key has more than one value, it returns the last value. ..."
Note: if you want all values for a key, you can call getlist(key) on the QueryDict.
So, the request.POST is a subclass of the dictionary but not a raw dictionary.
It is a subclass of the django MultiValueDict class which is a subclass of dict.
When I call get(key) to the Querydict it return the last value of the list. Sounds like the get method in Querydict overwrite the get method in the raw dictionary.
Yes ... that is what the __getitem__ method does.
By the way, I wonder why we need multiple value for one key.
It is because HTTP allows multiple values for a given parameter in a request. So if multiple values can be provided, django need to be able to make them available via the HttpRequest.POST object.
But since this is an unusual case, they decided to just give your code one value for each parameter ... unless it specifically asks for them all. Django is being helpful.
I am working on a tool for Jira using the python-jira library.
def find_ticket_key_by_name(search_string):
global jira
result = jira.search_issues('project=FSA and status != Done and summary ~ "HOST TESTER-APP:SERVICE1-SERVICECOUNT" order by createdDate', maxResults=1)
return result
The function above successfully returns a jira object
[<JIRA Issue: key=u'FSA-11', id=u'119060'>]
however if I attempt to print the key value
result.key
I get this error
AttributeError: 'ResultList' object has no attribute 'key'
I found the problem and posting solution in case somebody gets stuck like me.
In my case I am only returning one result and I assumed it will return one object.
This is not the case as indicated by the "ResultList" error. Even if you return 1 result the function will still return a list with 1 result.
What you are getting is a List in python, so try the following:-
result[0].key to get the key value
result[0].id to get id value.
You can always check type any data using type keyword, in your case it will be class 'jira.client.ResultList' which is a List object
If you want to get the key of issue you searched,
if result:
for issue in result:
print(issue.key)
It should help.
I wish to get an object in the following fashion:
Collection.objects.get(name='name', type='library', owner=owner, parent=parent)
Unfortunately type is a keyword as thus creates the following error:
KeyError at /forms/create_library
type
Is there a way to disambiguate the meaning of the word type to allow me to specify a field of that name?
Not tested:
Collection.objects.filter(
name__exact='name',
type__exact='library',
owner__exact=owner,
parent__exact=parent)
Query docs: http://docs.djangoproject.com/en/dev/topics/db/queries/
Also consider naming your field differently, mainly not with the same name as a builtin.
OK it turns out the problem was elsewhere. I was doing this in a form and thus using the self.cleaned_data dictionary of input values.
I was attempting to retrieve self.cleaned_data['type'] where in my previous simplification I stated the string 'library'. This was not in fact in the cleaned data of the form and thus threw a KeyError.
In django, when a URL is matched, the match group is passed as a second parameter to the view function, the first being a HttpRequest object. For example, with a URL patter like this
'/foo/(\d{2})/', 'app.views.handler'
the handler routine will have
def handler(request, value):
where value will contain a two digit number (as a string).
My question is: is value also contained in the request object, and if yes, how can I get it (of course, parsing the URL from the request object is not an option, too impractical).
Thanks
I'm not going to debate the merit of your idea. Just try to answer your question:
No there is no way, other than applying the regex to the URL again, to get at the url parameter.
Your view will be the first point where the parameter list will be available. Why don't you just create a wrapper object to encapsulate your request and your parameter list at that point?
Just pass that around...
Can you give any reason why you would need this?
I don't see why parsing the url path is 'impractical', given that you've already got a regexp that works, in your urlconf.