복붙노트

[PYTHON] 다른 html 페이지에 대한 링크는 어떻게 만듭니 까?

PYTHON

다른 html 페이지에 대한 링크는 어떻게 만듭니 까?

한 페이지에 다른 페이지로 제출할 양식이 있습니다. 두 번째 페이지에 대한 링크를 만드는 방법을 알 수 없습니다.

프로젝트 레이아웃 :

Fileserver/
    config.py
    requirements.txt
    run.py
    setup.py
    app/
        __init__.py
        static/
            css/
            img/
            js/
        templates/
            formAction.html
            formSubmit.html
            index.html

__init__.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    ip = request.remote_addr
    return render_template('index.html', user_ip=ip)

index.html :

<!DOCTYPE html>
<html lang="en">
<body>
    <ul>
        <li><a href="/formSubmit.html">Check Out This Form!</a>
    </ul>
</body>
</html>

localhost : 5000 /에 문제가없는 페이지를 볼 수 있습니다.

나는 또한 시도했다 :

<a href="{{ url_for('templates', 'formSubmit") }}"></a>

만큼 잘:

<a href="{{ url_for('formSubmit') }}"></a>

내가 뭘 놓치고 있니?

해결법

  1. ==============================

    1.url_for는 애플리케이션에 정의 된 경로에 대한 URL을 생성합니다. 특히 템플릿 폴더에서 제공되는 원시 html 파일이 없습니다 (또는 아마 존재하지 않아야합니다). 각 템플릿은 Jinja가 렌더링 한 템플릿이어야합니다. 표시하거나 양식을 게시하려는 각 위치는 응용 프로그램의 경로에 의해 처리되고 생성되어야합니다.

    url_for는 애플리케이션에 정의 된 경로에 대한 URL을 생성합니다. 특히 템플릿 폴더에서 제공되는 원시 html 파일이 없습니다 (또는 아마 존재하지 않아야합니다). 각 템플릿은 Jinja가 렌더링 한 템플릿이어야합니다. 표시하거나 양식을 게시하려는 각 위치는 응용 프로그램의 경로에 의해 처리되고 생성되어야합니다.

    이 경우 GET에서 양식을 렌더링하고 POST에서 양식 제출을 처리하는 경로를 하나 갖고 싶을 것입니다.

    __init__.py:

    from flask import Flask, request, url_for, redirect, render_template
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        return render_template('index.html')
    
    @app.route('/cool_form', methods=['GET', 'POST'])
    def cool_form():
        if request.method == 'POST':
            # do stuff when the form is submitted
    
            # redirect to end the POST handling
            # the redirect can be to the same route or somewhere else
            return redirect(url_for('index'))
    
        # show the form, it wasn't submitted
        return render_template('cool_form.html')
    

    templates / index.html :

    <!doctype html>
    <html>
    <body>
        <p><a href="{{ url_for('cool_form') }}">Check out this cool form!</a></p>
    </body>
    </html>
    

    templates / cool_form.html :

    <!doctype html>
    <html>
    <body>
        <form method="post">
            <button type="submit">Do it!</button>
        </form>
    </html>
    

    귀하의 양식과 경로가 실제로 무엇을하는지 모르겠으므로 이것은 단지 예일뿐입니다.

    정적 파일을 링크해야하는 경우 정적 폴더에 저장 한 후 다음을 사용하십시오.

    url_for('static', filename='a_picture.png')
    
  2. from https://stackoverflow.com/questions/27539309/how-do-i-create-a-link-to-another-html-page by cc-by-sa and MIT license