p4 no.4_form表单与登录页面

no.4_form表单与登录页面

1 安装模块 python-multipart

pip install python-multipart -i http://pypi.douban.com/simple --trusted-host=pypi.douban.com

2 修改main.py代码

# https://fastapi.tiangolo.com/advanced/templates/
# pip install python-multipart -i http://pypi.douban.com/simple --trusted-host=pypi.douban.com

#####################################
   
# -*- coding: UTF-8 -*-
from starlette.requests import Request
from fastapi import FastAPI, Form
from starlette.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates(directory="templates")


@app.post("/user/")
async def form_text(request: Request, username: str = Form(...), password: str = Form(...)):
    
    print('username',username)
    print('password',password)
    
    # return {'text_1':text_1 , 'text_2': text_2}
    return templates.TemplateResponse('index.html', {'request': request, 'username': username, 'password': password})


@app.get("/")
async def main(request: Request):
    return templates.TemplateResponse('post.html', {'request': request})


######################################

if __name__=='__main__':
    import uvicorn
    uvicorn.run(app,host='127.0.0.1',port=8000)

# uvicorn main:app --reload

3 新建 templates/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- <link rel="icon" href="../../favicon.ico"> -->

    <title>Signin Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> -->


    <!-- Custom styles for this template -->
    <!-- <link href="../static/css/signin.css" rel="stylesheet"> -->

  </head>

  <body>

    <div class="container">

      <h1>HELLO..{{ username }}</h1>
      <h1>HELLO..{{ password }}</h1>
    </div> <!-- /container -->


  </body>
</html>

4 新建templates/post.html

    <body>
        <div class="container">
            <form action="/user/" enctype="application/x-www-form-urlencoded" method="post">
                <label>username</label>
                <br>
                <input name="username" type="username" >
                <br>
                <label>password</label>
                <br>
                <input name="password" type="password" >
                <br><br>
                <input type="submit">
            </form>
            
        </div> <!-- /container -->
    </body>

5 测试

http://127.0.0.1:8000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值