将窗体的默认值通过Django推送到Flas

2024-10-05 14:24:38 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试让用户在遍历数据库中的所有问题之后选择一个要回答的问题。我想把他们选择的问题的ID放到session['select_q']变量中,以便在另一个页面上使用。我当前的代码抛出了一个400 Bad Request: The browser (or proxy) sent a request that this server could not understand.错误,我认为我没有正确地将值传递给会话。将选项的值传递给会话的最佳方法是什么,这样我就可以在单独的页面room.html上使用该值。也许有一个更好的方法来做这些以外的会议,但让我知道

pending.html代码:

{% for r in result %}
            <div class="uk-card uk-card-default uk-width-1-2@m">
                <div class="uk-card-header">
                    <div class="uk-grid-small uk-flex-middle" uk-grid>
                        <div class="uk-width-auto">
                            <img class="uk-border-circle" width="40" height="40" src="{{ url_for('static', filename='images/user_avatar.jpg')}}">
                        </div>
                        <div class="uk-width-expand">
                            <h3 class="uk-card-title uk-margin-remove-bottom">{{ r[9] }}</h3>
                            <p class="uk-text-meta uk-margin-remove-top">{{ r[4] }}</p>
                            <li><u>Time Submitted</u>: {{ r[5] }}</li>
                            <!--<li><u>Question ID</u>: {{ r[0] }}</li>-->
                            <!--GET TECH INFO<li><u>TECH ID</u>: {{ r[0] }}</li>-->
                        </div>
                    </div>
                </div>
                <div class="uk-card-footer">
                    <form action="/room/" method="post">
                            <!-- POST to session which set they are working with -->
                            <input type="hidden" class="uk-input uk-invisible" name="select_q" value="{{ r[0] }}">
                            <input class="uk-button uk-button-primary" type="submit" value="View">
                    </form>
                </div>

                {% if error %}
                    <span class="uk-label uk-label-danger">{{ error }}</span></p>
                {% endif %}
            </div>
        {% endfor %}

room.html代码:

<h3>{{ result[9] }}</h3>
        <p>{{ result[4] }}</p>
        <li><u>Time Submitted</u>: {{ result[5] }}</li>
        <li><u>Difficulty</u>: {{ result[3] }}</li>
        <li><u>Tags</u>: {{ result[10] }}</li>
        <!-- include tech info -->

        {% for t in thread %}
            <p>Thread Info:</p>
            <li><u>Question ID</u>: {{ t[0] }}</li>
            <li><u>Client ID</u>: {{ t[1] }}</li>
            <li><u>Technician ID</u>: {{ t[2] }}</li>
            <p><u>Body:</u><br> {{ t[4] }}</p>
        {% endfor %}


        <hr>
        <form action="/room/" method="post">
            <textarea class="uk-textarea" rows="10" placeholder="Enter your response here" name="body" value="{{ request.form.body }}"></textarea><br><br>
            <input class="uk-button uk-button-primary" type="submit" value="Comment">
        </form>

烧瓶代码:

@app.route('/room/', methods=['GET','POST'])
def room():
    error = ''
    try:
        c, conn = connection()
        clientcid = session['clientcid']
        qid = session['select_q']
        c.execute("SELECT * FROM tickets WHERE qid = (%s)", (qid,))
        result = c.fetchall()
        conn.commit()
        c.execute("SELECT * FROM threads WHERE cid = (%s) AND qid = (%s)", (clientcid, qid))
        thread = c.fetchall()
        conn.commit()
        #What to insert into the conversation
        if request.method == 'POST':
            clientcid = session['clientcid']
            body = request.form['body']
            c.execute("INSERT INTO threads (qid, cid, body) VALUES (%s, %s, %s)", (qid, clientcid, thwart(body)))
            conn.commit()
            c.close()
            conn.close()

        else:
            error = "Something is aloof."
        return render_template("room.html", thread = thread, result = result)

    except Exception as e:
        return(str(e))

@app.route('/pending/', methods=['GET','POST'])
def pending():
    error = ''
    try:
        result = ''
        c, conn = connection()
        clientcid = session['clientcid']
        c.execute("SELECT * FROM tickets WHERE cid = (%s) AND solved = 0", (clientcid,))
        result = c.fetchall()
        conn.commit()
        #what question to view and discuss
        if request.method == 'POST':
            clientcid = session['clientcid']
            ##grab the question the client chose, and use in the room
            session['select_q'] = request.form.get('select_q')
            return render_template("room.html")
        else:
            error = "Something is aloof."
        return render_template("pending.html", result = result)

    except Exception as e:
        return(str(e))

Tags: divformidrequestsessionhtmlerrorli