【发布时间】:2016-08-06 11:23:55
【问题描述】:
我正在尝试使用 Django 表单和模板将变量发送到 html。
这是我的代码...
class AdminView(TemplateView):
template_name = 'admin.html'
def admin(self, request):
template = 'admin.html'
data = Quiz.objects.all()
form = AdminForm(request.POST)
context = {"form": form}
context['admin'] = data
# return render(request, template, context)
return render_to_response(context)
这段代码有什么问题?怎么这个变量没有出现在网站上。
行 context = {"form", form} 已在我的 IDE 中突出显示为错误。
错误:此字典创建可以重写为字典文字。
这看起来怎么样?
class AdminView(TemplateView):
template_name = 'admin.html'
def post(self, request):
template = 'admin.html'
data = Quiz.objects.all()
form = AdminForm(request.POST)
context = {"form": form}
context['admin'] = data
return render(request, template, context)
可惜还是不行?
这是html代码...
{% if request.user.is_authenticated%}
{% if request.user.username == "teacher" %}
<!DOCTYPE html>
<html lang="en">
<head>
{% load staticfiles %}
<meta charset="UTF-8">
<title>Admin</title>
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}" />
</head>
<body>
{% include 'navbar.html' %}
Admin Test
{{ admin }}
</body>
</html>
{% endif %}
【问题讨论】:
标签: django python-3.x django-forms django-templates