【问题标题】:Django templates not loading without errorsDjango模板没有加载没有错误
【发布时间】:2021-05-01 14:32:47
【问题描述】:

我正在尝试在 Django 中加载我的模板,但它不会加载除 base.html 之外的任何内容,并且也没有显示任何错误。

我已经尝试了这些解决方案,但没有看到和改变:

Templates not loading in Django

Django Template not loading

Django template doesn't load

这是我的看法:

def all(request):
    lead_list = Lead.objects.all()
    print(lead_list)
    return render(request, "leads.html", {'leads': lead_list}) 

网址:

urlpatterns = [
    path('leads/', views.leads, name="leads"),
    path('newlead/', views.newlead, name="newlead"),
    path('leads/<int:pk>', views.lead_details, name="lead-details"),
    path('leads/<int:pk>/update', views.lead_update, name="lead-update"),
    path('leads/<int:pk>/delete', views.lead_delete, name="lead-delete"),
    path('all/', views.all, name="all"),
]

这里是 HTML:

    {% extends 'base.html' %}

{% block content %}

<h1>Hello World</h1>

{% for lead in leads %}
<div class="max-w-sm mx-auto bg-white dark:bg-gray-800 shadow-lg rounded-lg overflow-hidden">
    <img class="w-full h-56 object-cover object-center" src="https://images.unsplash.com/photo-1517841905240-472988babdf9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80" alt="avatar">   
    <div class="flex items-center px-6 py-3 bg-gray-900">
        <svg class="h-6 w-6 fill-current text-white" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path fill-rule="evenodd" clip-rule="evenodd" d="M17 21C15.8954 21 15 20.1046 15 19V15C15 13.8954 15.8954 13 17 13H19V12C19 8.13401 15.866 5 12 5C8.13401 5 5 8.13401 5 12V13H7C8.10457 13 9 13.8954 9 15V19C9 20.1046 8.10457 21 7 21H3V12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12V21H17ZM19 15H17V19H19V15ZM7 15H5V19H7V15Z"/>
        </svg>
        <h1 class="mx-3 text-white font-semibold text-lg">{{lead.status}}</h1>
    </div>
    <div class="py-4 px-6">
        <h1 class="text-xl font-semibold text-gray-800 dark:text-white">{{lead.first_name}} {{lead.last_name}}</h1>
        <p class="py-2 text-gray-700 dark:text-gray-400">{{lead.city}} {{lead.country}} </p>
        <div class="flex items-center mt-4 text-gray-700 dark:text-gray-200">
            <svg class="h-6 w-6 fill-current" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M14 11H10V13H14V11Z"/><path fill-rule="evenodd" clip-rule="evenodd" d="M7 5V4C7 2.89545 7.89539 2 9 2H15C16.1046 2 17 2.89545 17 4V5H20C21.6569 5 23 6.34314 23 8V18C23 19.6569 21.6569 21 20 21H4C2.34314 21 1 19.6569 1 18V8C1 6.34314 2.34314 5 4 5H7ZM9 4H15V5H9V4ZM4 7C3.44775 7 3 7.44769 3 8V14H21V8C21 7.44769 20.5522 7 20 7H4ZM3 18V16H21V18C21 18.5523 20.5522 19 20 19H4C3.44775 19 3 18.5523 3 18Z"/>
            </svg>
            <h1 class="px-2 text-sm">{{lead.age}}</h1>
        </div>
        <div class="flex items-center mt-4 text-gray-700 dark:text-gray-200">
            <svg class="h-6 w-6 fill-current" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path fill-rule="evenodd" clip-rule="evenodd" d="M16.2721 10.2721C16.2721 12.4813 14.4813 14.2721 12.2721 14.2721C10.063 14.2721 8.27214 12.4813 8.27214 10.2721C8.27214 8.063 10.063 6.27214 12.2721 6.27214C14.4813 6.27214 16.2721 8.063 16.2721 10.2721ZM14.2721 10.2721C14.2721 11.3767 13.3767 12.2721 12.2721 12.2721C11.1676 12.2721 10.2721 11.3767 10.2721 10.2721C10.2721 9.16757 11.1676 8.27214 12.2721 8.27214C13.3767 8.27214 14.2721 9.16757 14.2721 10.2721Z"/><path fill-rule="evenodd" clip-rule="evenodd" d="M5.79417 16.5183C2.19424 13.0909 2.05438 7.3941 5.48178 3.79418C8.90918 0.194258 14.6059 0.0543983 18.2059 3.48179C21.8058 6.90919 21.9457 12.606 18.5183 16.2059L12.3124 22.7241L5.79417 16.5183ZM17.0698 14.8268L12.243 19.8965L7.17324 15.0698C4.3733 12.404 4.26452 7.9732 6.93028 5.17326C9.59603 2.37332 14.0268 2.26454 16.8268 4.93029C19.6267 7.59604 19.7355 12.0269 17.0698 14.8268Z"/>
            </svg>
            <h1 class="px-2 text-sm">California</h1>
        </div>
        <div class="flex items-center mt-4 text-gray-700 dark:text-gray-200">
            <svg class="h-6 w-6 fill-current" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path fill-rule="evenodd" clip-rule="evenodd" d="M3.00977 5.83789C3.00977 5.28561 3.45748 4.83789 4.00977 4.83789H20C20.5523 4.83789 21 5.28561 21 5.83789V17.1621C21 18.2667 20.1046 19.1621 19 19.1621H5C3.89543 19.1621 3 18.2667 3 17.1621V6.16211C3 6.11449 3.00333 6.06765 3.00977 6.0218V5.83789ZM5 8.06165V17.1621H19V8.06199L14.1215 12.9405C12.9499 14.1121 11.0504 14.1121 9.87885 12.9405L5 8.06165ZM6.57232 6.80554H17.428L12.7073 11.5263C12.3168 11.9168 11.6836 11.9168 11.2931 11.5263L6.57232 6.80554Z"/>
            </svg>  
            <h1 class="px-2 text-sm">{{lead.email}}</h1>
        </div>
    </div>
</div>

{% endfor %}

{% endblock content %}

【问题讨论】:

    标签: html django django-views django-templates


    【解决方案1】:

    确保你的 base.html 中有内容块

    {% block content %}
    {% endblock content %}
    

    你的 base.html 中有这个块吗?

    【讨论】:

      猜你喜欢
      • 2013-10-11
      • 1970-01-01
      • 2017-04-26
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 2015-03-18
      • 2013-01-04
      相关资源
      最近更新 更多