【发布时间】:2017-07-01 23:02:37
【问题描述】:
我正在开发一个 django 网站(我的第一个网站),我试图在上下文中添加一个表单。我无法在模板中看到变量,所以我简化为字符串变量,但仍然没有结果。我不完全清楚自己做错了什么,所以我希望能得到一些帮助。
这是视图:
from django.shortcuts import render
from django.http import HttpResponse
from django.template.context import RequestContext
from forms import LoginForm
# Create your views here.
def index(request):
return HttpResponse("Index page for COMPANY portal")
def login(request):
lFormContext = RequestContext(request,{'mystring' : 'mystring'})
return render(request, 'login.html', lFormContext )
这是模板:
{% extends 'base.html' %}
{% load i18n %}
{% load debug_tags %}
{% block container %}
<div class="content">
{% variables %}
{% if form.errors %}
<div class="alert alert-danger">
<p><strong>{% trans "Oh snap!" %}</strong> {% trans "Please enter a correct username and password. Note that both fields are case-sensitive." %}</p>
</div>
{% endif %}
<form action="{% url 'login' %}" method="post" class="form-horizontal" role="form">{% csrf_token %}
<legend><span class="col-sm-offset-1">{% trans 'Log in' %}</span></legend>
{% for field in form.fields %}
<p>iterating over form.fields </p>
{% include 'registration/form_field.html' %}
{% endfor %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">{% trans 'Log in' %}</button>
<button type="reset" class="btn">{% trans 'Cancel' %}</button>
</div>
</div>
</form>
<p><a href="{% url 'resetPassword' %}">{% trans "Reset my password" %}</a></p>
<script type="text/javascript">
$(function(){
$(".alert-message").alert();
$('#mainForm').submit(function(){
$('#submit').button('loading');
})
});
document.forms[1].elements[2].focus();
</script>
</div>
{% endblock %}
我希望 {% variables %} 的输出显示对我在视图中添加的“mystring”的一些引用,但没有添加额外的变量。我有点需要弄清楚这一点。
这是 {% variables %} 的输出
['DEFAULT_MESSAGE_LEVELS', 'False', 'None', 'True', 'block', u'csrf_token', 'messages', 'perms', u'request', 'user', u'view']
我不确定到底出了什么问题——我正在正确地添加到上下文字典中。有没有我可能会丢失的设置?这似乎很基本。
这是我的模板设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/portal/templates/portal'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
非常感谢任何帮助或建议。谢谢。
【问题讨论】:
标签: python django django-templates django-views