简体   繁体   中英

Django 1.10 unable to get variable into the context

I'm working on a django site (my first) and I was trying to add a form to the context. I wasn't able to see the variable in my template, so I simplified to a string variable, and still no result. I'm not entirely clear on what I'm doing wrong so I was hoping to get some help.

Here's the view:

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 )

and here's the template:

{% 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 %}

I would expect the output from {% variables %} to show some reference to the 'mystring' that I added in the view, but there's no extra variable added. I somewhat need to figure this out.

Here's the output from {% variables %}

['DEFAULT_MESSAGE_LEVELS', 'False', 'None', 'True', 'block', u'csrf_token', 'messages', 'perms', u'request', 'user', u'view']

I'm not sure what's exactly wrong -- I'm adding to the context dictionary properly. Is there a setting that I could be missing? This seems pretty basic.

Here is my TEMPLATES setting:

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',
            ],
        },
    },
]

Any help or suggestions are greatly appreciated. Thanks.

render takes a dict, not a RequestContext; it generates the RequestContext itself.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM