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
80 views
in Technique[技术] by (71.8m points)

html - How exactly does the form "action" attribute in HTML5 send the data to the backend for a live website?

I am sorry if this is a really stupid question but it feels that I am missing something when it comes to understanding how exactly the action attribute works. Most of the tutorials I have come across run on localhost so it makes sense when you simply provide action = "dir/register.py" as it is located on the same machine and the POST request knows where to go.

But, when we make a live website for example lets say I make an EC2 instance, install Ubuntu and use that as the backend- I would get an IP address. I will make this static and then register with a DNS to tell the internet that my website name xyz.com is on static IP x:x:x:x.

Now, say a user requests for the registration page, my routes will send the necessary html, css, js pages back to the browser for the users to enter details. What I don't understand is how does the browser know to send it back to the backend on submit when the action attribute specified by me is still action = "dir/register.py". Does the browser automatically remember where the request came from and internally handle it by appending the necessary IP to specify the absolute path for my script to handle registration or do I have to change the action attribute to action = "x:x:x:x/dir/register.py" when making a live website?

EDIT 1: Code for url that processes form data (probably):

@users.route("/register", methods = ["GET","POST"])
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(username = form.username.data, email = form.email.data, password = hashed_password)
        db.session.add(user)
        db.session.commit()
        flash(f'Account created for {form.username.data}!', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title = 'Register', form = form)
question from:https://stackoverflow.com/questions/65913250/how-exactly-does-the-form-action-attribute-in-html5-send-the-data-to-the-backe

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

1 Answer

0 votes
by (71.8m points)

Always ask what you don't know. It's Ok. And about your question, you can specify paths in html page to be relative or absolute. Absolute urls start with http:// or https:// and contain full address of document like : http://example.com/blabla. Relative urls have part of the path relative to current url, like if you are on http://example.com/index.html and there is a relative path in action attribute like dir/xyz.py automatically browser understand that this resource is on http://example.com/dir/xyz.py. But another thing about your question, you cant pass a python file as an action url of a form. There should be a url that processes form data.

UPDATE: As you asked about url processing data, I write this update: When you set action parameter of html form to /something, and let's say your method is POST, you're telling browser to submit your form data to that path with POST method. I hope you know what are methods and differences but if you don't no problem, google it and there are a lot of content about it. As I said, that path should response to requests. A rare python file can't handle any request and browser don't run python file also and if browser could run python file, it wouldn't help cause python file should run on your server. Flask and other python frameworks like Django help you build web applications easily by python. You should run python main.py if I remember correctly, and then a server will start and paths like localhost:<port>/register will be available. I hope this help you. Good luck.


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

...