Question

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 :-/

Was it helpful?

Solution

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>

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top