Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django queries return empty strings

I have a problem with my django app, i'm created two Blog objects, one in mysql and another one with my view. I see them both in my database but i can't see them when i get them with a query.

I'm using django 1.9, python 2.7, apache 2.2 with mod_wsgi

Here is my view and my template.

def list_view(request):
    blogs = Blog.objects.filter(published=True)

return render_to_response('blog/list.html',
    {
        "blogs": blogs,
    },
    context_instance=RequestContext(request))


{% for blog in blogs %}
    <h2>{{blog.title}}</h2>
    <p>{{blog.content}}</p>
    <span>{% trans 'Writen by' %} : {{blog.writer.last_name}} {{blog.writer.first_name}}</span> - <span>{% trans 'Published on' %} {{blog.date_published}}</span>
{% endfor %}

The query gets me a list with 2 Blog objects inside, but they are empty. My template just shows Writen By - Published on twice.

But i can log in and print all my user informations.

Any idea what could be the problem or how i could solve it ? Thanks a lot !

EDIT : add models.py

class Blog(models.Model):
    title = models.CharField(_("Title"), max_length=255,   null=False, blank=False)
    content = models.TextField(_("Content"), null=True, blank=True)

    writer = models.ForeignKey(User, verbose_name=_('Writer'), blank=False, null=False)

    published = models.BooleanField(_("Pusblished"), default=False)
    date_published = models.DateTimeField(_("Date published"))

    def __str__(self):
        return '%s' % (self.title)

    def __init__(self, *args, **kwargs):
        super(Blog, self).__init__()

1 Answers

When you overrode Blog.__init__(), you did not send *args and **kwargs to the parent. Django Model magic needs those parameters. If you're not doing anything in __init__() you could just remove that all together.

If you want to keep it, do something like:

def __init__(self, *args, **kwargs):
    super(Blog, self).__init__(*args, **kwargs)
like image 112
Andrei Avram Avatar answered Apr 12 '26 04:04

Andrei Avram