【问题标题】:Flask form not submitting but working under another scenario烧瓶表单未提交但在另一种情况下工作
【发布时间】:2022-01-02 07:19:01
【问题描述】:

我的烧瓶网站正在进行中,但每次单击 SubmitField 时它都没有做任何事情。 我按照类似的方法提交了一个更小的表单,它有效,但这不起作用。

routes.py

from flask.scaffold import _matching_loader_thinks_module_is_package
from slotinfo import app
from flask import render_template,request,redirect
from slotinfo.models import Item
from slotinfo.models import Projects
from slotinfo.forms import AddProjectForm, ReserveSlotForm
from slotinfo import db
from slotinfo.models import Projects

@app.route('/') 
@app.route('/home')
def home_page():
    form = AddProjectForm()
    return render_template('home.html',form=form)


@app.route('/rack1')
def rack_one():
    items = Item.query.filter_by(rack_number=1)
    form = AddProjectForm()
    rform = ReserveSlotForm()

    return render_template('rack1.html',items=items,form=form,rform=rform)

@app.route('/rack2')
def rack_two():
    items2 = Item.query.filter_by(rack_number=2)
    form = AddProjectForm()
    return render_template('rack2.html',items=items2,form=form)

@app.route('/rack3')
def rack_three():
    items3 = Item.query.filter_by(rack_number=3)
    form = AddProjectForm()
    return render_template('rack3.html',items=items3,form=form)

@app.route('/witbe1')
def wit_rack1():
    items4 = Item.query.filter_by(rack_number=4)
    form = AddProjectForm()
    return render_template('rack4.html',items=items4,form=form)

@app.route('/addproject', methods=['GET','POST'])
def add_project():
    form = AddProjectForm()
    name=form.projectname
    name2 = request.form['projectname']
    x=Projects(projname=name2)
    db.session.add(x)
    db.session.commit()
    return redirect(request.referrer)

@app.route('/reserve',methods=['GET','POST'])
def reserve_r1():
    rform1=ReserveSlotForm()
    name = request.rform1['reserver_name']
    # if form1.validate_on_submit():
    #   name=form1.reserver_name
    print(name)
    return redirect(request.referrer)

在上面的代码中, rack1.html 是我创建提交表单的页面。同样在 base.html 中,我使用了一个较小的形式,它可以在 add_project() 函数中使用。但是当我尝试对 rack1.html 做同样的事情时,它不会提交任何东西。我使用函数 reserve_r1()

rack1.html

{% extends 'base.html' %}
{% block title %}
  Rack 1 Page
{% endblock %}
{% block content %}
<br>
<div class="container-fluid">
    <div class="jumbotron-fluid">
        <div class="card" id = "rack-table1" style="color: black;">
            <div class="card-header">
                <h3>Storm Rack One</h3>
            </div>

            <div class="card-body">
                <div class="row">
                    <div class="col">
                        <h4 class="card-title">Details of the rack</h4>
                    </div>
                    <div class="col">
                        <button class="btn btn-outline btn-success float-right badge-pill" style="margin-left: 5px;" data-toggle="modal" data-target="#reserveslot">Reserve</button>
                        <button class="btn btn-outline btn-danger float-right badge-pill" style="margin-bottom: 2px;">Release</button>
                    </div>
                </div>
                <br>
                <table class="table table-hover table-light table-bordered" style="color: black;">
                    <thead style="text-align: center;">
                        <tr style="text-align: justify;">
                            <th scope="col" style="width: 8rem;">Rack Number</th>
                            <th scope="col" style="width: 8rem;">Slot Number</th>
                            <th scope="col" style="width: 10rem;">Project</th>
                            <th scope="col">Name</th>
                            <th scope="col" style="width:5rem;">Days</th>
                            <th scope="col" style="width:12rem;">Date of Reserving</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in items %}
                            <tr style="text-align: justify;">
                                <td>{{item.rack_number}}</td>
                                <td>{{item.slot_number}}</td>
                                <td>{{item.project}}</td>
                                <td>{{item.name}}</td>
                                <td>{{item.days_taken}}</td>
                                <td>NA</td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div id="reserveslot" class="modal fade" style="color: black;">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">  
              <!-- <button type="button" class="close" data-dismiss="modal">×</button>   -->
              <h4 class="modal-title" style="text-align: left;">Reserve Slot</h4>  
            </div>  
            <div class="modal-body">
                <form method="POST" action = "{{url_for('reserve_r1')}}">
                    {{rform.rack_number.label()}}
                    {{rform.rack_number(class="form-control",placeholder="1-4")}}
                    {{rform.slot_number.label()}}
                    {{rform.slot_number(class="form-control",placeholder="1-4")}}
                    {{rform.project_Name.label()}}
                    {{rform.project_Name(class="form-control",placeholder="Project for reserving")}}
                    {{rform.reserver_name.label()}}
                    {{rform.reserver_name(class="form-control",placeholder="Who is reserving the slot ?")}}
                    {{rform.days_reserve.label()}}
                    {{rform.days_reserve(class="form-control",placeholder="For how many days ?")}}
                    {{rform.description.label()}}
                    {{rform.description(class="form-control",placeholder="Why do you need this slot ?")}}
                    {{rform.submit_slot(class="btn btn-lg btn-block btn-primary") }}
                        <br>
                </form>
            </div>
            <div class="modal-footer" style="text-align: center;">
              <button type="button" class="btn btn-primary" data-dismiss="modal" style="text-align: center;">Close</button>
            </div>           
          </div>  
        </div>  
      </div>  
</div>


<style>
.divider{
    width:7px;
    height:auto;
    display:inline-block;
}
</style>
{% endblock %}

【问题讨论】:

    标签: python html flask bootstrap-4 jinja2


    【解决方案1】:

    删除action 属性周围的多余空格。

    <form method="POST" action="{{url_for('reserve_r1')}}">
    

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      相关资源
      最近更新 更多