简体   繁体   中英

Django render doesn't seem to pass dictionary to template

Update: I fixed it by uninstalling and re-installing Python/Django. Wasn't sure what was wrong.

I am trying to print out values from a dictionary to a html template using render (also tried render_to_response) but it doesn't seem to be working. The HTML template does not recognize any of the values I tried to pass into it.

Django code

def search(request):
    context = RequestContext(request)

    results2= [{'link':12, 'title':34, 'summary':56}, {'link':121, 'title':341, 'summary':561}]

    print results2  #is printed in command prompt, so this function is working
    return render_to_response('home.html', {'results2': results2}, context)
    #tried using render as well

HTML Template

{% extends "base.html" %}
{% load staticfiles %}


    <form class="form-signin span8" id="user_form" method="post" action="">
        {% csrf_token %}
        <!-- Display the search form elements here -->
        <input type="text" size="50" name="query" value="" id="query" />
        <input class="btn btn-primary" type="submit" name="submit"    value="Search" placeholder="Put Search"/>
        <br />
    </form>        

    <p>{{results2}}</p>
    <p>test1</p>
    <p>{{link}}</p>
    <p>test2</p>

    <div style="clear: both;">
        <ol>
        {% for result in results2 %}
            <li>
                <strong><a href="{{ result.link }}">{{ result.title }}</a>                  </strong><br />
                <em>{{ result.summary }}</em>
            </li>
        {% endfor %}
        </ol>
    </div>

In the command promp when the server is running and the function is called, it's printing 'results2', so the function 'search' is definitely working. However, the variables don't appear to be rendered.

View source for rendered page (equivalent to what's above):

    <form class="form-signin span8" id="user_form" method="post" action="">
        <input type='hidden' name='csrfmiddlewaretoken' value='Y4PfA0x8jH67jWUlTJfjbMzk0EAouPrp' />
        <!-- Display the search form elements here -->
        <input type="text" size="50" name="query" value="" id="query" />
        <input class="btn btn-primary" type="submit" name="submit" value="Search" placeholder="Put Search"/>
        <br />
    </form>

    <p></p>
    <p>test1</p>
    <p></p>
    <p>test2</p>


    <p>test2</p>
    <div style="clear: both;">
        <ol>

        </ol>
    </div>

So the list of dictionaries 'results2' is not being passed to the template. I'm not getting any errors.

It looks like your template expects results2 to be a list of dicts, while you're passing a single dict. Try something like render_to_response('home.html', {'results2': [results2, ]}, context)

Does this happen only for this template and are all the other templates working fine? If not, check up your settings file. Make sure that you have defined the TEMPLATE_CONTEXT_PROCESSORS. It should look something like this..

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP

TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request',)

Other than this, I don't see any other problem with the code.

What version of Django are you running? If it's 1.8, the context processors were moved:

Changed in Django 1.8:

Built-in template context processors were moved from django.core.context_processors to django.template.context_processors in Django 1.8.

So in your settings file, under TEMPLATE_CONTEXT_PROCESSORS , it should look like this:

(
    "django.contrib.auth.context_processors.auth",
    "django.template.context_processors.debug",
    "django.template.context_processors.i18n",
    "django.template.context_processors.media",
    "django.template.context_processors.request",
    "django.template.context_processors.static",
    "django.template.context_processors.tz",
    "django.contrib.messages.context_processors.messages"
)

尝试这样更改您的退货声明,

return render_to_response('home.html', {'results2': results2}, context_instance = context)

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