我想在单击锚标记时传递锚标记内的文本,并在定向页面上显示相同的文本

2024-09-29 23:23:55 发布

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

这是我的discover.html文件

<form action="POST">
    <div class="swipe-area">
      <div class="swiper-container mySwiper">
        <div class="swiper-wrapper">
          {% for topic in topics %}
            <div class="swiper-slide">
              <a href="{% url 'content' %}" style="color: white; text-decoration: none;">
                <h1 name="headings" data-tooltip="{{ topic.description }}" data-tooltip-location="bottom">
                  {{ topic.heading }}
                </h1>
              </a>
            </div>
          {% endfor %}
        </div>
        <div class="swiper-button-next"></div>
        <div class="swiper-button-prev"></div>
      </div>
      <!-- <div class="swiper-button-next"></div>
      <div class="swiper-button-prev"></div> -->
    </div>
  </form>

我想将h1标记的值传递到链接所带的页面,当单击该链接时,该值为{{ topic.heading }}

              <a href="{% url 'content' %}" style="color: white; text-decoration: none;">
                <h1 name="headings" data-tooltip="{{ topic.description }}" data-tooltip-location="bottom">
                  {{ topic.heading }}
                </h1>
              </a>

下面是我的views.py文件

def content(request):
    heading = request.GET['headings']
    content = Search.objects.all().filter(title__contains = heading)
    context = {'content': content}
    return render(request, 'search/content.html', context)

所以我想在我的content.html页面中使用这个变量值。 请告诉我如何解决这个问题,我已经被困在这里两个星期了


Tags: 文件divformdatatopicrequesthtmlbutton
1条回答
网友
1楼 · 发布于 2024-09-29 23:23:55

您应该在链接href中添加标题

      <a href="{% url 'content' %}?heading={{topic.heading}}" style="color: white; text-decoration: none;">
        <h1 name="headings" data-tooltip="{{ topic.description }}" data-tooltip-location="bottom">
          {{ topic.heading }}
        </h1>
      </a>

这将输出类似于<a href="http://localhost:8000/content?heading=foobar"></a>的内容,然后当用户单击此链接时,heading参数将添加到request.GET

您需要相应地修改视图heading = request.GET['heading']

相关问题 更多 >

    热门问题