Newbie: problems searching a model's field from a template

27 views
Skip to first unread message

joy

unread,
Jan 14, 2013, 5:44:06 AM1/14/13
to django...@googlegroups.com
it's the first time i try using the model property so probably i'm doing something wrong.
This is the model file:

from django.db import models

# Create your models here.

class Pop(models.Model):
    name = models.CharField(max_length=10)
    content = models.TextField(blank=True, null=True)

    def __unicode__(self):
        return self.name

class Text(models.Model):
    pag = models.CharField(max_length=20)
    content = models.TextField(blank=True, null=True)
    popup = models.ManyToManyField(Pop, blank=True, null=True)

    def __unicode__(self):
        return self.pag

And this is the template:

<!doctype html>
<html lang="it">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=800">
  <link rel="icon" href="/static/img/standard.gif" sizes="16x16" type="image/gif">
  <link rel="icon" href="/static/img/iphone.png"   sizes="57x57" type="image/png">
  <link rel="icon" href="/static/img/vector.svg"   sizes="any"   type="image/svg+xml">
  <link rel="stylesheet" href="http://joy.indivia.net/css/style.css">
  <script type="text/javascript" src="http://joy.indivia.net/js/cookie.js"></script>
  <script type="text/javascript" src="http://joy.indivia.net/js/html5shiv.js"></script>
  <script type="text/javascript" src="http://joy.indivia.net/js/avgrund.js"></script>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.7/prototype.js"></script>
 
</head>
  <body>
    <header>
<h1>MADE IN HUMAN</h1>
</header>
{% for t in text %}
         <article id="content" class="avgrund-contents"> {{ t.content|safe }}</article>
{% endfor %}
         <div class="avgrund-cover"></div>
{% if text.popup_set.all %}
{% for p in text.popup_set.all %}
 <script>
            var openDialog = function(){
                Avgrund.show( "{{p.name}}" );
            };
            function closeDialog() {
                Avgrund.hide();
            };
            document.observe('dom:loaded', function(){
                $('content').on('click', 'a', openDialog());
            };
        </script>
        <aside id="{{p.name}}" class="avgrund-popup">
                    {{ p.content|safe }}
                    <button onclick="javascript:closeDialog();">Close</button>
                </aside>
{% endfor %}{% endif %}
<footer>
        <dl>
        </dl>
    </footer>
</body>
</html>


After the if statement probably nothing gets executed, so probably text.popup_set.all doesn't work, even if there are pops in the popup field of the text passed to the html file by a render to response call....
Someone that speaks django dialect better than me can explain me what i'm doing wrong?
Thanks


Daniel Roseman

unread,
Jan 14, 2013, 8:14:38 AM1/14/13
to django...@googlegroups.com
It's hard to tell without seeing your view. What exactly is `text` here? Is it a single Text instance, or a queryset of many of them? The first `for` loop seems to imply the latter, but then you try and access text.popups which implies it's a single instance.

In any case, you don't need the _set. That's just for reverse relationships, whereas you're accessing a relationship that's defined on the Text model itself, so you just use the field name: {% for p in text.popup.all %}.

Note that the `if` block is unnecessary and will cause an extra database query for no reason. If the queryset is empty, the for loop will simply not enter.
--
DR.
Reply all
Reply to author
Forward
0 new messages