django-selenium TypeError: 'str' object is not callable - django

Doing a simple test of a clicking dropdown and seeing if the menu is displayed.
dropdown_user = self.browser.find_element_by_id('dropdown-user')
dropdown_user.click()
expanded = dropdown_user.get_attribute("aria-expanded")
self.assertTrue= (expanded)
settings = self.browser.find_element_by_id('dropdown-user-settings')
self.assertTrue(settings.is_displayed())
Gives me this error when I run the test. I cant figure why settings is a str.
self.assertTrue(settings.is_displayed())
TypeError: 'str' object is not callable

I can't comment (not enough rep) or I would - could you post the whole stack trace? The line self.assertTrue= (expanded) looks like it could feasibly cause an issue.
Edit: I think you're assigning the value of the variable expanded to self.assertTrue, then when you try to call self.assertTrue you're trying to call a string, rather than a function. Remove the line self.assertTrue=(expanded) and replace it with self.assertEqual(expanded, 'true').
Edit 2 to explain in more depth as requested:
The value of expanded is a string - probably 'true', if your dropdown is expanded.
Writing self.assertTrue=(expanded) is the same (in this case) as writing self.assertTrue=expanded. You're assigning the value of the variable expanded (which is a string) to the variable self.assertEqual - it is no longer a function, it's a string!
self.assertTrue(True) # fine
self.assertTrue=('Woops!') # the value of self.assertTrue is now the
# string 'Whoops!'
print(self.assertTrue)
>'Woops!'
self.assertTrue(True) # you're trying to call a string here
> TypeError: 'str' object is not callable
In python, there's nothing to stop you from assigning any type to any variable, because it's dynamically typed.

Related

How I solve this error with the split function?

I have written the following code and seem to be getting an error, can anyone help? I am using python 3.
Input code:
def fruits(h):
ingredients = fruits.split()
print(ingredients)
print(fruits("apples berries honey"])
Output:
ingredients = fruits.split()
AttributeError: 'function' object has no attribute 'split'
You are calling a method called split() with a function name.
split() function only exists as a method of String type. Thus, you must call and invoke split() with a String type variable not a function.
Maybe, the argument h is a String. So you might want to do h.split()
And you also have the closing bracket, when you call
print(fruits("apples berries honey"])
please modify this code to down below:
print(fruits("apples berries honey"))

TYPO3 Fluid - Template Paginate

Another question for today, but I'm fixing some errors in my extension, and that's the last one.
I had this error many times:
Core: Exception handler (WEB): Uncaught TYPO3 Exception: #145451971: Supplied file object type TYPO3\CMS\Fluid\ViewHelpers\Widget\PaginateViewHelper must be QueryResultInterface
or ObjectStorage or be an array. | UnexpectedValueException thrown in file /var/www/typo3_src_elts/typo3/sysext/fluid/Classes/ViewHelpers/Widget/PaginateViewHelper.php
Maybe I should put a condition when the array is null to display an error message, but where? in controller or template?
If your variable is null, you must define an empty array in the controller befor accessing it with fluid.
If its an array, you can simple add an condition to the template, to check if it's empty:
<f:if condition="{array -> f:count()} > 0">
<f:then><!-- pagination --></f:then>
<f:else><!-- do something when empty --></f:else>
</f:if>
You can also try to check if an variable exist:
<f:if condition="{array}">
<f:then><!-- pagination --></f:then>
<f:else><!-- do something when empty --></f:else>
</f:if>
This should work for null variables, but I dont checked this out.

TypeError: expected string or bytes-like object User.id

I'm trying to register an new Transaction object on the DB using Django, but I'm having TypeError: expected string or bytes-like object when I try to do user_id = user.id I can't really understand why this is happening, since I do the same steps when registering a new Bank object (as shown on prints below). I've tried to debug and the local variables have the correct value, I've also tried to cast user.id with string or int, but none of them worked.
traceback console error create Transaction method create Bank method
models.py
Firstly, please don't post code or errors as images; they are text, they should be posted as text in the question.
However I don't see anything in any of those snippets that suggest the error is with the user - that line is probably highlighted because it's the last in that multi-line call.
Rather, the error looks to be in the reference to date.today - if that's the datetime.date class, then today is a method, which you would need to call:
Transaction.objects.create(date=date.today(), ... )
Or, since that field has a default anyway, you could leave out the date attribute from the create call altogether.

Getting "AttributeError: 'str' object has no attribute 'regex'" in Django urls.reverse

As in the title, I'm getting
AttributeError: 'str' object has no attribute 'regex'
I've seen many valid answers to this, but none of them applied to my code as they identified issues in the urls.py file. My issue is in the views.py file.
I get the exception when using reverse to generate a redirect URL:
HttpResponseRedirect(reverse('changetracker:detail', args))
When I use 'changetracker:detail' in other functions, I don't get the exception.
I'm answering this to share knowledge as I didn't see this particular root cause identified already.
The problem turned out to be that I should be using a keyword argument 'args=args' to pass in URL arguments, not a positional argument:
HttpResponseRedirect(reverse('changetracker:detail', args=args))
Using the positional argument (position 2) caused reverse to use that argument as the URL list and thus raise the AttributeError.

Error converting Unix timestamp to int - ValueError: invalid literal for int() with base 10: ''

I am writing a Django Framework app that receives Unix Timestamps as parameters in an Ajax call and uses them to query a database. My issue is in converting the timestamp strings to Python ints, which throws ValueError: invalid literal for int() with base 10: ''. This is the snippet:
start = request.GET.get('start','')
start = int(start)
I tried printing the timestamp and copying the output into a shell for the same operation, which completed successfully:
>>> int(1485939600)
1485939600
The error suggests it's being passed an empty string yet I can print it. Not sure what the problem could be?
The second argument of get is the default value if the query parameter 'start' does not exist
start = request.GET.get('start','')
Empty string cannot be transformed in an Int.
start = request.GET.get('start',0)
Will default to 1970 if there is no start param
Thanks all, I found the problem was elsewhere, the AJAX call was being made without parameters on pageload, passing an empty string to int().