我正在尝试将views.py中的变量传递给我的login.html模板。
这是我的views.py
from django.shortcuts import render
# Create your views here.
def home(request):
numbers = [1, 2, 3, 4, 5]
context = {
'numbers': numbers
}
return render(request, 'accounts/login.html', {'title': 'Login'}, context)
这是我的login.html
{% extends 'accounts/base.html' %}
{% block content %}
<h1>Welcome</h1>
<h5 class="text-muted">Please log in to continue</h5>
<button href="#" class="btn btn-outline-primary mt-1">Log in</button>
{{ numbers }}
{% endblock content %}
这也是我应用程序中的urls.py文件
from . import views
from django.urls import path
urlpatterns = [
path('', views.home, name="home"),
path('home/', views.home, name="home"),
]
当地址为localhost:8000 / myappname / home时,我希望列表[1、2、3、4、5]显示在login.html上
答案 0 :(得分:4)
由于某些原因,您有两个词典。您只需要一个,其中应该包含要传递的所有值。
def home(request):
numbers = [1, 2, 3, 4, 5]
context = {
'numbers': numbers,
'title': 'Login'
}
return render(request, 'accounts/login.html', context)