Issue
I have the following code:
@app.route("/create", methods=["POST", "GET"])
@login_required
def create():
if request.method == "POST":
list = []
for x in request.form.items():
if x[0] == "date":
datefc = datetime.strptime(x[1], "%Y-%m-%d")
elif x[0] == "frequency":
frequency = x[1]
elif x[0] == "submit":
continue
else:
list.append(x)
adate = datetime.today()
if frequency == "Weekly":
nContactTime = datefc + timedelta(days=7)
elif frequency == "Monthly":
nContactTime = datefc + timedelta(days=30)
elif frequency == "Quarterly":
nContactTime = datefc + timedelta(days=90)
people.insert_one({list, "frequency": frequency, "parentrecord": current_user.user_json['_id'], "dateadded": adate.strftime("%x"),"datefc": datefc, "nextContact": nContactTime})
flash('Your record has been entered successfully.')
return redirect(url_for('profile'))
I use for x in request.form.items()
to loop through a dynamically generated form. Users create custom fields that can be added. I then add the results to a list of which an example of a print out is:
[('name', 'David'), ('phone', '90'), ('email', 'inf'), ('company', 'Ran'), ('Kids Names', 'Kids Names'), ('Kids Birthday', '2022-08-03'), ("Wife's Email", '[email protected]')]
My issue is when I attempt to run the code I get a syntax error:
Traceback (most recent call last):
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\__main__.py", line 3, in <module>
main()
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 986, in main
cli.main()
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 567, in main
return super().main(*args, **kwargs)
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1055, in main
rv = self.invoke(ctx)
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\click\decorators.py", line 84, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 848, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 279, in __init__
self._load_unlocked()
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 313, in _load_unlocked
self._app = rv = self.loader()
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 373, in load_app
app = locate_app(import_name, None, raise_if_not_found=False)
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 214, in locate_app
__import__(module_name)
File "C:\Users\David PC\Desktop\VS Code Python\Flask Site\app.py", line 150
people.insert_one({list, "frequency": frequency, "parentrecord": current_user.user_json['_id'], "dateadded": adate.strftime("%x"),"datefc": datefc, "nextContact": nContactTime})
^
SyntaxError: invalid syntax
Process finished with exit code 1
I am fairly confident the issue arises that my list is improperly formated (or my insert is). I've tried converting it using json_dumps
, etc and nothing seems to work. Assistance would be appreciated!
Solution
The issue I was facing above was because I was using a list vs a dictionary. I modified my code to be:
@app.route("/create", methods=["POST", "GET"])
@login_required
def create():
if request.method == "POST":
nList = {}
for x in request.form.items():
if x[0] == "date":
datefc = datetime.strptime(x[1], "%Y-%m-%d")
elif x[0] == "frequency":
frequency = x[1]
nList.update({"frequency":x[1]})
elif x[0] == "submit":
continue
else:
nList.update({x[0]:x[1]})
adate = datetime.today()
nList.update({"datefc": adate})
nList.update({"parentrecord": current_user.user_json['_id']})
nList.update({"dateadded": adate.strftime("%x")})
if frequency == "Weekly":
nContactTime = datefc + timedelta(days=7)
nList.update({"nextContact": nContactTime})
elif frequency == "Monthly":
nContactTime = datefc + timedelta(days=30)
nList.update({"nextContact": nContactTime})
elif frequency == "Quarterly":
nContactTime = datefc + timedelta(days=90)
nList.update({"nextContact": nContactTime})
people.insert_one(nList)
flash('Your record has been entered successfully.')
return redirect(url_for('profile'))
And the problem was solved. The key was using nList = {}
and nList.update()
vs using nList = []
and nList.append()
Answered By - David
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.