1

I am just starting developing with Django, and I'm slowly falling in love. However, i am quite a noob still and have a hard time understanding why this particular model isn't working, so here I am.

I have this model in models.py:

class projectBuildTests(models.Model):
    project = models.CharField(max_length = 50)
    build = models.CharField(max_length = 10)
    testName = models.CharField(max_length = 50)

My view.py goes like this:

def home(request):
    projects = projectBuildTests.objects.all()
    return render_to_response('testrunner/home.html')

in my home.html I am trying to show it like this:

<label>Project:</label><br>
<select class="project_test_selector" multiple="multiple" size="10">
    {% for project in projects %}
    <option>{{ project.text }}</option>
    {% endfor %}
</select>

I am managing the models data with the django admin. It's registered like this:

admin.site.register(projectBuildTests)

In the actual admin-panel the model is showing a bit strange. It gives me a bunch of sub-objects instead of a normal column list which I can append data to. I don't know if I'm making sense here :-/

3
  • In your view function, you don't pass the objects variable to the template, so it does nothing. Commented Feb 12, 2014 at 10:20
  • Also as a style guide - don't use camel-case for python class names projectBuildTests should be ProjectBuildTests Commented Feb 12, 2014 at 10:25
  • I'll fix that, thanks :) Commented Feb 12, 2014 at 10:29

2 Answers 2

3

In your views.py, you need to pass projects queryset in context of response.

from django.template import RequestContext
def home(request):
    extra_context = {}
    projects = projectBuildTests.objects.all()
    extra_context['projects'] = projects
    return render_to_response('testrunner/home.html', extra_context, context_instance=RequestContext(request))

And in Your Template:

<label>Project:</label><br>
<select class="project_test_selector" multiple="multiple" size="10">
    {% for project in projects %}
        <option>{{ project.testName }}</option>
    {% endfor %}
</select>
Sign up to request clarification or add additional context in comments.

4 Comments

This will throw an error because extra_context isn't defined.
:D . forget to initialize dictionary . Edited .
I see. Now it gives me a Nameerror: global name 'extra_context' is not defined
You could just use render, then you don't have to fiddle with the request context yourself.
0

To edit a little Priyank Patel's response.. Django shorcuts are very useful. For example, you can import render:

from django.shortcuts import render

Then just return:

return render(request, 'testrunner/home.html', extra_context)

2 Comments

when would this be more useful than a normal rendertoresponse? Or rather, what's the difference there?
It's the same thing. 'render' is just a shortcut for render_to_response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.