【问题标题】:Django HTML: how do I make a "go home" link?Django HTML:如何创建“回家”链接?
【发布时间】:2021-10-18 05:04:44
【问题描述】:

这是我项目的文件结构。

我在 testpage.html 中添加了一个链接,我希望它能够将用户带回到根目录(本地服务器上的http://127.0.0.1:8000),该目录配置为使用 index.html 作为 pages > templates > 下的主页索引。

testpage.html 如下。链接在第 3 行(当前为空):

 <h1 class='list'>Classes</h1>
        {% if mydata %}
        <a href="">Go home</a>
        {% endif %}
       {% for k in mydata  %}
        <table border="1">
            <thead>
                <th>Team</th>
                <th>Name</th>
                <th>ClassCode</th>
            </thead>
            <tr>
                <td>{{k.Team}}</td>
                <td>{{k.Name}}</td>
                <td>{{k.ClassCode}}</td>
            
            </tr>
            
            
        </table>
        {% endfor %}
{% endblock %}

【问题讨论】:

    标签: python python-3.x django django-templates


    【解决方案1】:

    在您的应用程序 urls.py 中,您应该有类似的内容

    from django.urls import path
    
    from . import views
    
    urlpatterns = [
        #...
        path('home/', views.home, name='home'),
        #...
    ]
    

    然后在模板中,你可以有

    <a href="{% url 'home' %}">Go home</a>
    

    这里home 是路径的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 2016-01-30
      • 2023-03-21
      • 2018-04-27
      • 2017-11-22
      相关资源
      最近更新 更多