Python } {%extends“layout.html”%} {%block content%} 索引:{{photo_Index}} 文件名:{{image} {%include”/HTML片段/calendar.HTML“%} {%include”/HTML片段/favorite_button.HTML“%} 逆时针旋转照片 顺时针旋转照片 {图像列表中图像行的百分比|批次(3)%} {图像中图像的%u行%} {%endfor%} {%endfor%} {%endblock%}

Python } {%extends“layout.html”%} {%block content%} 索引:{{photo_Index}} 文件名:{{image} {%include”/HTML片段/calendar.HTML“%} {%include”/HTML片段/favorite_button.HTML“%} 逆时针旋转照片 顺时针旋转照片 {图像列表中图像行的百分比|批次(3)%} {图像中图像的%u行%} {%endfor%} {%endfor%} {%endblock%},python,flask,Python,Flask,还有日历位calendar.html {% block topscripts %} <link rel="stylesheet" type="text/css" href= "{{ url_for('static',filename='styles/calendar.css') }}"> <script> $(function() { $("#datepicker").datepicker({dateFormat:

还有日历位calendar.html

{% block topscripts %}
    <link rel="stylesheet" type="text/css" href= "{{ url_for('static',filename='styles/calendar.css') }}">
    <script>
        $(function() {
            $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
        });
    </script>    
{% endblock %}

{% block content %}
<form method="post" action="{{ url_for('default_template') }}">
    <input type="hidden" name="calendar-form">
    <p>
        Date: <input type="text" id="datepicker"  name='go-to-date'
        {% if request.form['go-to-date'] is not none %}
            value="{{request.form['go-to-date']}}"
        {% endif %}

        ></p>
    <input type="submit">
</form>
{% endblock %}

{% block endscripts %}

{% endblock %}
{%block-topscript%}
$(函数(){
$(“#datepicker”).datepicker({dateFormat:'yy-mm-dd'});
});
{%endblock%}
{%block content%}

日期:

{%endblock%} {%block endscripts%} {%endblock%}
您需要在下一个/上一个表单和表格链接中传递足够的信息,以重新应用日期过滤器。您的日历表单与下一个/上一个导航表单是分开的,在提交另一个导航表单时,浏览器不会将其中的信息序列化。单击
以同时容纳查询参数和表单数据

您不仅需要在收到
POST
请求时查找此参数,还需要查找所有请求:

if 'go-to-date' in request.values:
    date = request.values['go-to-date']
    image_list = get_files_on(date)
    photo_index_obj.set_number(0)
    if len(image_list) == 0:
        image_list = ["no_images_for_date.jpg"]
else:
    image_list = image_list or image_urls
num_images = len(image_list) - 1

if request.method == 'POST':
    # ...
然后生成包含以下参数的URL:

{%-set url_-params={'go-to-date':如果请求,则请求.values['go-to-date']}。值['go-to-date']否则{}-%}
{图像中图像的%u行%}
{%endfor%}
对于
next/previous
表单,只需添加一个带有当前
go to date
值的隐藏输入字段:


{%-if request.values['go-to-date']-%}
{%-endif-%}

逆时针旋转照片 顺时针旋转照片
那里有很多事情让我动脑,但是
photo\u index=photo\u index\u obj.index
看起来可能是个不错的钩子。你不能跟踪你以前提供的图像索引并将其传递给模板吗?@roganjosh-是的,对所有的代码感到抱歉。我想说得更清楚一些,就像过去我不小心漏掉了关键的部分一样……但是是的,
photo\u index
是一个很好的使用方法。。。那么,我是否会将索引传递到HTML模板,然后以某种方式在Flask routes.py中再次检索?我的方法,实际上这完全是我自己偶然发现的,没有教授(我的意思是可能不是最佳实践),将对象存储在一个会话中,跟踪它最后提供的服务。另一个选项是作为获取的隐藏变量传递给模板。任何能够维持国家的东西;我怀疑分页方法在这里是适用的,只是有点重复。@roganjosh-如果您注意到,我将
image\u list
发送到
index.html
模板,因此html页面确实有照片列表。因此,当我第一次使用日历过滤器时,它只正确地传入这些图像。我想你的第一个评论是说,我可以在重新加载页面时,将列表“拉”回routes.py,是吗?我绞尽脑汁想怎么做。我想我可以在每个
a href
url\u中添加
image\u list
参数,就像我在开始时所说的那样,但这似乎不起作用。有没有更具体的方法?哦,天哪!!太近了!当我第一次加载页面时,我可以使用next/prev按钮浏览所有照片。但是,在我单击表中的URL后,会加载特定的图像(很好),但我不能再使用next/prev循环。页面只是停留在同一个图像上。当我选择一个日期时,我可以首先滚动浏览…有点,它变得有点不稳定。我正在回顾你说的话,并试图理解它,但即兴说,你知道我可能忽略了什么吗?@BruceWayne:仔细看看你浏览器中的URL。该链接将您从
/
转到
/date\u image.jpg
。您需要使用“下一步/上一步”按钮处理此情况;表单将发布到当前URL,因此不会在浏览器中更改。你必须在那一点上改变访问者的方向。啊,我明白你的意思。所以,我现在尝试做
@brucewayn:基本上,web服务器将每个请求视为一个全新的、独立的响应查询。每个浏览器都保存当前“状态”、位置和构成页面的其他信息。如果您想给访问者下一个响应,您需要确保请求包含足够的信息,以检测正确的下一个响应应该是什么。如何将数据从浏览器传输到服务器取决于您。您现在有2个表单和一组用户可以交互的链接,这些表单和链接现在将足够的信息传输回服务器,以决定如何respond@BruceWayne:我以前没有注意到这一点,但实际上您在服务器上保存了太多的状态,在
photo\u index\u obj
global中。如果两个不同的用户访问您的站点,其中一个用户单击“下一步”,另一个用户会想,为什么下次加载您的站点时,他们会看到不同的照片。不知不觉中,多个用户疯狂地点击“下一个”和“上一个”会变得完全困惑。您希望将该信息保存在浏览器中。URL是一种很好的方法。
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{{ title }}</title>
    <link rel="stylesheet" type="text/css" href= "{{ url_for('static',filename='styles/index.css') }}">
    <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
</head>
<body>
{% extends "layout.html" %}
{% block content %}
    <h3>Index: {{ photo_index }}</h3>
    <h3>Filename: {{ image }}</h3>
    <div id="calendar-selector">
    {% include "/HTML Snippets/calendar.html" %}
    </div>
    <div class='image-container' id='image'>
        {% include "/HTML Snippets/favorite_button.html" %}
        <img src="{{ url_for('images.static', filename=image) }} " id="the-photo">
    </div>
    <div class='button-container' id='buttons'>
        <form action="" method="post">
            <input type="hidden" name="prev-next-buttons">
            <input type="submit" value="Prev photo" name='prev-photo'>
            <input type="submit" value="Next photo" name='next-photo'>
            <input type="submit" value="Random photo" name='random-photo'>
            <br/>
            <button type='button' id='rotate-button' onclick="rotateMeCounterClockwise('#the-photo')">Rotate Photo CounterClockwise</button>
            <button type='button' id='rotate-button' onclick="rotateMeClockwise('#the-photo')">Rotate Photo Clockwise</button>
        </form>
    </div>
    <div class='table-container'>
        <table id='image-list' name='select-from-table'>
            {% for image_row in image_list | batch(3) %}
            <tr>
                {% for image in image_row %}
                <td><a href="{{ url_for('default_template', chosen_image=image) }}"> {{ image }} </a></td>
                {% endfor %}
            </tr>
            {% endfor %}
        </table>
    </div>
{% endblock %}
</body>
</html>
{% block topscripts %}
    <link rel="stylesheet" type="text/css" href= "{{ url_for('static',filename='styles/calendar.css') }}">
    <script>
        $(function() {
            $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
        });
    </script>    
{% endblock %}

{% block content %}
<form method="post" action="{{ url_for('default_template') }}">
    <input type="hidden" name="calendar-form">
    <p>
        Date: <input type="text" id="datepicker"  name='go-to-date'
        {% if request.form['go-to-date'] is not none %}
            value="{{request.form['go-to-date']}}"
        {% endif %}

        ></p>
    <input type="submit">
</form>
{% endblock %}

{% block endscripts %}

{% endblock %}
if 'go-to-date' in request.values:
    date = request.values['go-to-date']
    image_list = get_files_on(date)
    photo_index_obj.set_number(0)
    if len(image_list) == 0:
        image_list = ["no_images_for_date.jpg"]
else:
    image_list = image_list or image_urls
num_images = len(image_list) - 1

if request.method == 'POST':
    # ...