#๐ Need help with Flask site.
19 messages ยท Page 1 of 1 (latest)
@rugged sage
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Please DM me if you can help
We don't help in DMs here
Show your code and describe the problem you're having?
My frontend needs to be able to communicate with my back end but it doesn't really work.
I am having this error now.
TypeError: createUser() missing 1 required positional argument: 'user_data'
Hey @rugged sage!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
Remember to use code blocks, like in the bot advise
#file name is plant_frontend_server.py
from flask import Flask, request, render_template, make_response
from plant_api_shim import create_user
from config import CONFIG
import logging
import os
# Set up the directory
script_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(script_dir)
# Initialize the Flask app
app = Flask(__name__, static_url_path='/static')
# Set up logging
logging.basicConfig(filename='z_app_log.txt', level=logging.DEBUG)
@app.route('/user', methods=['GET', 'POST'])
def create_new_user():
if request.method == 'GET':
return render_template('create_user.html')
elif request.method == 'POST':
try:
# Extract user data from the form
user_data = {
"username": request.form.get('username'),
"password": request.form.get('password')
}
logging.debug(f"Received user data: {user_data}")
# Call the create_user function from plant_api_shim.py
status_code = create_user(user_data['username'], user_data['password'])
# Check the status code and return appropriate response
if status_code == 201:
return render_template('create_user.html', user_added=user_data), 201
else:
return render_template('create_user.html', error='Failed to create user'), status_code
except Exception as e:
logging.error(f"Error creating user: {e}")
return make_response('Internal Server Error', 500)
return make_response("Invalid request", 400)
# Run the app
if __name__ == '__main__':
frontend_ip = CONFIG["frontend"]["listen_ip"]
frontend_port = int(CONFIG["frontend"]["port"])
logging.info(f"Starting frontend server on {frontend_ip}:{frontend_port}")
app.run(host=frontend_ip, port=frontend_port, debug=True)
Show the full traceback also
print('Hello, world!')
#file name is plant_api_shim.py
import requests
from config import CONFIG
import logging
# Set up logging
logging.basicConfig(level=logging.DEBUG)
def create_user(username, password):
try:
# Prepare the user data
user_data = {
"username": username,
"password": password
}
# Send the POST request to the API
response = requests.post(f"{CONFIG['api']['url']}/user", json=user_data)
# Raise an exception for HTTP errors
response.raise_for_status()
# Log success and return status code
logging.info("User created successfully")
return response.status_code
except requests.exceptions.RequestException as e:
# Log the error and return a server error code
logging.error(f"Error in create_user: {e}")
return 500 # Return a server error code
!traceback
Please provide the full traceback for your exception in order to help us identify your issue.
While the last line of the error message tells us what kind of error you got,
the full traceback will tell us which line, and other critical information to solve your problem.
Please avoid screenshots so we can copy and paste parts of the message.
A full traceback could look like:
Traceback (most recent call last):
File "my_file.py", line 5, in <module>
add_three("6")
File "my_file.py", line 2, in add_three
a = num + 3
~~~~^~~
TypeError: can only concatenate str (not "int") to str
If the traceback is long, use our pastebin.
@rugged sage Without the full error we can only guess as to what is happening.
The traceback will often reference a line number in the code you can look at.
In your case the error is TypeError: createUser() missing 1 required positional argument: 'user_data' which may imply that you are calling a function with an incorrect number of arguments.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.