【问题标题】:Django 1.10 unable to get variable into the contextDjango 1.10 无法将变量放入上下文
【发布时间】: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>
                  &nbsp;<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


    【解决方案1】:

    render 接受一个字典,而不是一个 RequestContext;它自己生成 RequestContext。

    def login(request):
        lFormContext = {'mystring' : 'mystring'}
        return render(request, 'login.html',  lFormContext)
    

    【讨论】:

    • 丹尼尔,感谢您的回答。我试过你的建议,改成字典,但在模板中仍然看不到变量。我还在模板中描述了请求对象,但在那里看不到变量。我认为设置丢失或不正确。会是这样吗? TIA。鲍勃
    • 不,不涉及任何设置。你确定这是正确的观点吗?您使用的是什么 URL,您的 urls.py 是什么样的?是否有任何其他视图可以呈现该模板? (老实说,您的原始代码无论如何都会给出 TypeError。)
    • 这是迄今为止我创建的唯一视图:/portal/accounts/login,这是 urls.py 中的相应行:url(r'^accounts/login$', TemplateView.as_view(template_name="login.html"), name='login'),
    • 问题出在我的 urls.py 文件中....感谢 Daniel Roseman 为我指明正确的方向!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 2014-08-15
    • 2019-07-28
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多