如何在django中创建命名锚点以获取页面的特定部分

ana*_*and 1 html django

我正在创建一个"Q&A"网站.在问题页面上可以看到它的所有答案(带分页).我还需要从其他页面直接回答(在页面中).我已经尝试实现命名锚点但无法直接到页面部分.

答案的get_absolute_url()以下列形式返回url:

www.example.com/question-id/question-slug?page=no#aID <!-- EDITED -->
Run Code Online (Sandbox Code Playgroud)

一个示例网址:

www.example.com/question-10/what-is-this?page=2#a20   <!-- EDITED -->
Run Code Online (Sandbox Code Playgroud)

并在HTML中:

    {% autopaginate answers 10 %}
    {% for answer in answers %}
      <div>
        <a name="a{{answer.id}}">Answer</a> <!-- EDITED -->
        {# answer body goes here#}
      </div>
    {% endfor %}
    {% paginate %}
Run Code Online (Sandbox Code Playgroud)

它会重定向到正确的页面,但不会重定向到正确的页面部分.

编辑:Result! When clicked, After going to the page section, it bounces to the bottom of the page

T.J*_*der 7

一个令人惊讶的鲜为人知的事实是,锚点可以转到页面上带有ID的任何元素 ; 它不一定是一个锚元素.所以你可以把ID放在你的身上div:

<div id="a_{{answer.id}}">
    <span>Answer</span>
    {# answer body goes here#}
</div>
Run Code Online (Sandbox Code Playgroud)

(显然,在答案标题周围使用任何有意义的元素; span上面仅用于说明.)

据我所知,使用锚点没有任何问题(虽然有内容的锚点往往表现出类似链接的行为 - 翻转等等 - 如果你不小心你的CSS),但作为一个选项,你有这也是另一种选择.