Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

python 3.x - function in Django behaves differently

I have a function named localtime(ltz, /) to calculate users' local time. Outside of Django it works, but I get localtime takes 0 positional arguments but 1 was given error inside of Django.

Here's the function UTILITY.PY

def localtime(ltz, /):
   utc_now = datetime.datetime.now(tz=pytz.UTC)
   return utc_now.astimezone(pytz.timezone(ltz))

VIEWS.PY

    ....
    qs = Users.objects.filter(id=self.request.user).values()
    for val in qs:
        local_time = localtime(val['time_zone'])
        print(local_time)
    ....



    Traceback (most recent call last):
    File "../env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
        response = get_response(request)
    File "../env/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "../env/lib/python3.8/site-packages/django/views/generic/base.py", line 73, in view
        return self.dispatch(request, *args, **kwargs)
    File "../env/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
        return bound_method(*args, **kwargs)
    File "../env/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
        return view_func(request, *args, **kwargs)
    File "../env/lib/python3.8/site-packages/django/views/generic/base.py", line 101, in dispatch
        return handler(request, *args, **kwargs)
    File "../env/lib/python3.8/site-packages/django/views/generic/edit.py", line 172, in post
        return super().post(request, *args, **kwargs)
    File "../env/lib/python3.8/site-packages/django/views/generic/edit.py", line 142, in post
        return self.form_valid(form)
    File "../users/views.py", line 148, in form_valid
        local_tz = localtime(val['time_zone'])

    Exception Type: TypeError at /timesheet/
    Exception Value: localtime() takes 0 positional arguments but 1 was given

Can anyone help figure out why I'm getting localtime takes 0 positional arguments but 1 was given error in Django? Thank you in advance.

question from:https://stackoverflow.com/questions/65851531/function-in-django-behaves-differently

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...