Reputation: 57
I am new to using Django. I have a function in views.py that computes the profit and prints it to a webpage in Django. I store the profit in a dictionary by the name of context
and then store it in a session:
#We want to calculate hourly profit
def profitmanagement(request):
#Profit has already been computed
context={'TotalHourlyProfit':TotalProfit}
#We will pass this dictionary to our ProfitManagement.html template. This will display our total profit.
request.session['context'] = context #Save the dictionary using a session to use in another function
return render(request,"ProfitManagement.html",context)
Now I have another function in views.py that will be triggered every hour using APScheduler. It should set the TotalHourlyProfit
to zero, and then output it to the webpage. It is as given below:
#Sets the hourly profit to zero at each hour
def ClearHourlyProfit():
context = request.session.get('context') #Loads the dictionary computed in profitmanagement(). This is returning an error
context['TotalHourlyProfit']=0 #Set the hourly profit to zero.
#print("Hourly profit function has context:",context)
return render(request,"ProfitManagement.html",context)
It returns the error:
AttributeError: module 'urllib.request' has no attribute 'session'
Is there a way by which I can pass the changed value of TotalHourlyProfit
to my webpage? I will be very grateful to anyone who can point me in the right direction.
Edit:
Upon request, I am including the traceback error before request is passed as a parameter and after request is passed as a parameter:
Before request:
Job "ClearHourlyProfit (trigger: interval[0:00:10], next run at: 2023-02-04 13:58:07 PKT)" raised an exception
Traceback (most recent call last):
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\apscheduler\executors\base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "C:\Users\taimo\Documents\Visual Studio Code Workloads\Eziline Project\web_project\POS\views.py", line 93, in ClearHourlyProfit
context = request.session.get('context') #Loads the dictionary computed in profitmanagement(). This is returning an error
AttributeError: module 'urllib.request' has no attribute 'session'
After request:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\threading.py", line 1016, in _bootstrap_inner
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
autoreload.raise_last_exception()
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\management\__init__.py", line 398, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\apps\registry.py", line 124, in populate
app_config.ready()
File "C:\Users\taimo\Documents\Visual Studio Code Workloads\Eziline Project\web_project\POS\apps.py", line 9, in ready
scheduler.start()
File "C:\Users\taimo\Documents\Visual Studio Code Workloads\Eziline Project\web_project\scheduler\scheduler.py", line 13, in start
scheduler.add_job(func=views.ClearHourlyProfit, trigger='interval', seconds=10) #Running this function every hour.
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\apscheduler\schedulers\base.py", line 438, in add_job
job = Job(self, **job_kwargs)
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\apscheduler\job.py", line 49, in __init__
self._modify(id=id or uuid4().hex, **kwargs)
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\apscheduler\job.py", line 180, in _modify
check_callable_args(func, args, kwargs)
File "C:\Users\taimo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\apscheduler\util.py", line 391, in check_callable_args
raise ValueError('The following arguments have not been supplied: %s' %
ValueError: The following arguments have not been supplied: request
Upvotes: 0
Views: 73
Reputation: 8837
You need to pass request
as an argument in ClearHourlyProfit()
function as well since it is a view function and also use {}
empty dict if context
does not exist so:
def ClearHourlyProfit(request):
context = request.session.get('context', {})
context['TotalHourlyProfit']=0
request.session['context'] = context
return render(request,"ProfitManagement.html",context)
Upvotes: 0