搜索
首页后端开发Python教程使用 HTMX 和 Django 创建待办事项应用程序,部分无限滚动

这是该系列的第 7 部分,我将在其中记录我使用 Django 学习 HTMX 的过程,其中我们将按照 HTMX 的文档来实现待办事项的无限滚动功能。

如果您想查看该系列的其余部分,请查看 dev.to/rodbv 以获得完整列表。

更新部分模板以加载多个项目

当我们实现无限滚动时,我们将必须返回几个待办事项(项目的下一个“页面”)并将它们加载到我们当前拥有的部分模板中。这意味着稍微改变我们的部分模板的组成方式;目前的设置如下图所示,其中部分模板负责渲染单个待办事项:

Creating a To-Do app with HTMX and Django, part infinite scroll

我们想要反转顺序,使部分围绕 for 循环:

Creating a To-Do app with HTMX and Django, part infinite scroll

让我们在模板 core/templates/index.html 中执行交换:


    Soon we will get back to the template to add the hx-get ... hx-trigger="revealed" bit that performs the infinite scroll, but first let's just change the view to return several items instead of one on the toggle and create operations:

... previous code 

def _create_todo(request):
    title = request.POST.get("title")
    if not title:
        raise ValueError("Title is required")

    todo = Todo.objects.create(title=title, user=request.user)

    return render(
        request,
        "tasks.html#todo-items-partial", # 



<p>检查内容的测试仍然通过,并且页面看起来相同,因此我们很好地实现无限滚动本身。</p>

<h2>
  
  
  实现无限滚动
</h2>

<p>在模板上,我们需要向 /tasks 设置一个 hx-get 请求,其中 hx-trigger="revealed" ,这意味着只有当元素即将进入屏幕上可见时才会触发 GET 请求;这意味着我们希望将其设置在列表中最后一个元素之后,并且我们还需要指示要加载哪个“页面”数据。在我们的例子中,我们将一次显示 20 个项目。</p>

<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173613850692024.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="Creating a To-Do app with HTMX and Django, part infinite scroll"></p>

<p>让我们相应地更改模板:<br>
</p>

<pre class="brush:php;toolbar:false">    

    There's an if next_page_number check around the "loading" icon at the bottom of the list, it will have two purposes: one is to indicate when we're loading more data, but more importantly, when the loader is revealed (it appears on the visible part of the page), it will trigger the hx-get call to /tasks, passing the page number to be retrieved. The attribute next_page_number will also be provided by the context

    The directive hx-swap:outerHTML indicates that we will replace the outerHTML of this element with the set of

  • s we get from the server, which is great because not only we show the new data we got, but we also get rid of the loading icon.

    We can now move to the views file.

    As a recap, here's how the GET /tasks view looks like by now; it's always returning the full template.

    @require_http_methods(["GET", "POST"])
    @login_required
    def tasks(request):
        if request.method == "POST":
            return _create_todo(request)
    
        # GET /tasks
        context = {
            "todos": request.user.todos.all().order_by("-created_at"),
            "fullname": request.user.get_full_name() or request.user.username,
        }
    
        return render(request, "tasks.html", context)
    

    上面的代码已经做了改动,就是按照最新的待办事项优先排序;既然我们期望有一个很长的列表,那么在底部添加新项目并将其与无限滚动混合是没有意义的 - 新项目最终将混合在列表的中间。

    我们现在需要区分常规 GET 请求和 HTMX 请求,为此我们将仅返回待办事项列表和部分模板。有一个名为 django-htmx 的库,它非常方便,因为它使用 request.htmx 等属性和所有 hx-* 属性的值扩展了请求参数,但目前这有点过分了;现在让我们检查 HTMX 标头,并使用 Django 分页器处理分页。

    # core/views.py
    
    ... previous code
    
    PAGE_SIZE = 20
    
    ...previous code
    
    @require_http_methods(["GET", "POST"])
    @login_required
    def tasks(request):
        if request.method == "POST":
            return _create_todo(request)
    
        page_number = int(request.GET.get("page", 1))
    
        all_todos = request.user.todos.all().order_by("-created_at")
        paginator = Paginator(all_todos, PAGE_SIZE)
        curr_page = paginator.get_page(page_number)
    
        context = {
            "todos": curr_page.object_list,
            "fullname": request.user.get_full_name() or request.user.username,
            "next_page_number": page_number + 1 if curr_page.has_next() else None,
        }
    
        template_name = "tasks.html"
    
        if "HX-Request" in request.headers:
            template_name += "#todo-items-partial"
    
        return render(request, template_name, context)
    

    我们做的第一件事是检查页面参数,如果不存在则将其设置为 1。

    我们检查请求中的 HX-Request 标头,这将告知我们传入的请求是否来自 HTMX,并让我们相应地返回部分模板或完整模板。

    这段代码肯定需要一些测试,但在此之前让我们先尝试一下。看一下网络工具,当页面滚动时如何触发请求,直到到达最后一页。您还可以看到动画“正在加载”图标短暂显示;我已将网络速度限制为 4g,以使其可见时间更长。

    Creating a To-Do app with HTMX and Django, part infinite scroll

    添加测试

    最后,我们可以添加一个测试来确保分页按预期工作

    
    

      Soon we will get back to the template to add the hx-get ... hx-trigger="revealed" bit that performs the infinite scroll, but first let's just change the view to return several items instead of one on the toggle and create operations:

    ... previous code 
    
    def _create_todo(request):
        title = request.POST.get("title")
        if not title:
            raise ValueError("Title is required")
    
        todo = Todo.objects.create(title=title, user=request.user)
    
        return render(
            request,
            "tasks.html#todo-items-partial", # 
    
    
    
    <p>现在就这样了!这是迄今为止我使用 HTMX 遇到的最有趣的事情。这篇文章的完整代码在这里。 </p>
    
    <p>对于下一篇文章,我正在考虑使用 AlpineJS 添加一些客户端状态管理,或者添加“截止日期”功能。再见!</p>
    
    
              
    
                
            

以上是使用 HTMX 和 Django 创建待办事项应用程序,部分无限滚动的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
您如何切成python阵列?您如何切成python阵列?May 01, 2025 am 12:18 AM

Python列表切片的基本语法是list[start:stop:step]。1.start是包含的第一个元素索引,2.stop是排除的第一个元素索引,3.step决定元素之间的步长。切片不仅用于提取数据,还可以修改和反转列表。

在什么情况下,列表的表现比数组表现更好?在什么情况下,列表的表现比数组表现更好?May 01, 2025 am 12:06 AM

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/删除,2)储存的二聚体和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

如何将Python数组转换为Python列表?如何将Python数组转换为Python列表?May 01, 2025 am 12:05 AM

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,请考虑performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。

当Python中存在列表时,使用数组的目的是什么?当Python中存在列表时,使用数组的目的是什么?May 01, 2025 am 12:04 AM

choosearraysoverlistsinpythonforbetterperformanceandmemoryfliceSpecificScenarios.1)largenumericaldatasets:arraysreducememoryusage.2)绩效 - 临界杂货:arraysoffersoffersOffersOffersOffersPoostSfoostSforsssfortasssfortaskslikeappensearch orearch.3)testessenforcety:arraysenforce:arraysenforc

说明如何通过列表和数组的元素迭代。说明如何通过列表和数组的元素迭代。May 01, 2025 am 12:01 AM

在Python中,可以使用for循环、enumerate和列表推导式遍历列表;在Java中,可以使用传统for循环和增强for循环遍历数组。1.Python列表遍历方法包括:for循环、enumerate和列表推导式。2.Java数组遍历方法包括:传统for循环和增强for循环。

什么是Python Switch语句?什么是Python Switch语句?Apr 30, 2025 pm 02:08 PM

本文讨论了Python版本3.10中介绍的新“匹配”语句,该语句与其他语言相同。它增强了代码的可读性,并为传统的if-elif-el提供了性能优势

Python中有什么例外组?Python中有什么例外组?Apr 30, 2025 pm 02:07 PM

Python 3.11中的异常组允许同时处理多个异常,从而改善了并发场景和复杂操作中的错误管理。

Python中的功能注释是什么?Python中的功能注释是什么?Apr 30, 2025 pm 02:06 PM

Python中的功能注释将元数据添加到函数中,以进行类型检查,文档和IDE支持。它们增强了代码的可读性,维护,并且在API开发,数据科学和图书馆创建中至关重要。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境