Ask coding questions

← Back to all posts
Replit db setting value error
Yoplayer1py (9)

Hi;
If you see in the repl you will see in main.py on line 31 I used replitdb to store the username. but it shows :

Traceback (most recent call last):
  File "main.py", line 27, in <module>
    app.run(host='0.0.0.0', port=8080)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 922, in run
    run_simple(t.cast(str, host), port, self, **options)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/werkzeug/serving.py", line 1008, in run_simple
    inner()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/werkzeug/serving.py", line 948, in inner
    srv = make_server(
  File "main.py", line 31
    db["user_id"] = user_id, # asking on replit.com/ask
    ^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

and my code for that line (just that lines in which I think the error is)

    user_id = request.headers['X-Replit-User-Id'],
    uid = generate_password_hash(user_id, "sha256"),
    db["user_id"] = uid, 

\\\\\\\## THANKS IN ADVANCE ##////////////////

Comments
hotnewtop
Coder100 (18930)

Right here:

that's not valid syntax, because you put this inside render_template ... only variable arguments are allowed, you can't put statements there.
did you mean:

user_id = db["user_id"]
Yoplayer1py (9)

@Coder100 no. I changed my code a bit. uid is a hash of the user_id. I am trying to add the hash to the database.

Here's the new code:

I added it to a variable named user_name_hashed . Or should I do it after rendering the template?

Yoplayer1py (9)

@Yoplayer1py but I get an syntax error:

  File "main.py", line 31
    user_name_hashed = db["user_id"] = uid, # asking on https://replit.com/talk/ask/Replit-db-setting-value-error/141847
                                     ^
SyntaxError: invalid syntax
QuantumCodes (26)

I didnt quite understand... does the flask to py work? Dont you need to do @app.route("/whatwvevr",method=["POST","GET"])? And for line 31, db['whatever'] = user sets the key whatever's value : user (your webpage doesnt wakeup for me.. sorry)

QuantumCodes (26)

i think multiple var assignment doesnt work on html.. try massigning them separately @Yoplayer1py

Yoplayer1py (9)

@QuantumCodes ? like:

user_name_hashed = db["user_id"]
user_name_hashed["user_id"] = uid
QuantumCodes (26)

Yeah! If thats what u tried to accomplish in that line. Try it and see? @Yoplayer1py

QuantumCodes (26)

@Yoplayer1py waait...db["user_id"] 's value is a dictionary or just a string? If its a dictionary and user_idkey is in the dict then it should work. If its a string/float/integer, then user_name_hashed is a variable and you cannot run the user_name_hashed["user_id"] as this is a dictionary command.

QuantumCodes (26)

And as @Coder100 said, you cannot set a dictionary value to something in the flask brackets. @Yoplayer1py

Yoplayer1py (9)

@QuantumCodes so should I do it outside render_template ?

Yoplayer1py (9)

@Yoplayer1py I forgot we can't add anything after return. Maybe I should add it to another function ?

QuantumCodes (26)

Do it outside render template. Just above the return render_template @Yoplayer1py

Yoplayer1py (9)

@QuantumCodes hmm.. aren't the variables assigned after it's rendered the template ?

QuantumCodes (26)

@Yoplayer1py uhmmm... i dont know what are yountryong to accomplish. Based on the code you commented a while back, i assume that this might work...

user_name_hashed = db["user_id"]
uid = user_name_hashed```
Idk whether you want the uid to be set user_name_hashed or vice versa. Try this, if it didnt work, then idk how to fix
Yoplayer1py (9)

@QuantumCodes I am trying to hash a username which I get by replauth .

user_name_hashed is a var which creates the key user_id I want the valuse to be uid which is the hash of the variable

user_id = request.headers['X-Replit-User-Id']

I make the hash using werkzeug:

Hope you understand it now.

QuantumCodes (26)

You cant set db value in flask brackets(the return code) you can only do before or after it. The 3rd line of that SS suggests u r setting db and a variable to hashed one @Yoplayer1py

Yoplayer1py (9)

@QuantumCodes means? what should I do. Any code after the return statement won't work right?

QuantumCodes (26)

Look through code here: https://replit.com/@QuantumCodes/Repl-Pinger#Keep.py
. I made that. It uses HTML to get data and also uses db to save. See whether you get ur answer there... @Yoplayer1py

Yoplayer1py (9)

@QuantumCodes I'll check and answer later

QuantumCodes (26)

@Yoplayer1py OHHH i understand what are you trying!! but i dont understand how can you do this without method=[post,get] ok.... does without hashing work?

Yoplayer1py (9)

@QuantumCodes yeah. I think the problem is in assigning the var in line 27