开发者

Django data not showing

开发者 https://www.devze.com 2023-02-27 07:26 出处:网络
I am building a website just for fun of learning programming more fully, but have run into an issue. I am trying to make a status update area of the user homepage where the latest status update for th

I am building a website just for fun of learning programming more fully, but have run into an issue. I am trying to make a status update area of the user homepage where the latest status update for the current user will display up top of their page(like Facebook). However the function I have made to retrieve the data isnt seeming to display it at all. I cant seem to see what the problem might be considering all other functions have been built similarly to this one. What I have is listed below:

def show_user_homepage(request):
    user1 = request.user
    status = StatusUpdate.objects.filter(status_user=user1).order_by('status_开发者_开发百科date_time')[1:]
    context = RequestContext(request,{'user':user1,'status':status})
    return render_to_response('users/index.html',context_instance=context)

Template section for status display for function is:

        <div id="current-status">
            <p>
                Status:
                <span class="italics">
                    {% if status.status_id %}
                        {{ status.status_text }}
                    {% else %}
                        No status currently available
                    {% endif %}
                </span>
            </p>
        </div>

The class for the Status is:

class StatusUpdate(models.Model):
    status_id = models.AutoField(primary_key=True)
    status_user = models.ForeignKey(User)
    status_text = models.CharField(max_length=2000)
    status_date_time = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
    return '%s' % self.status_text

No matter what Ive tried, the status part in the template always says there isnt a status available. Thanks for any help.


list[1:] will return elements from 1 to the end. So your status variable is not a single status but a list of statuses and a list does not have the attribute status_id

to get the last one use [-1]

Update:

You can also use django function latest(http://docs.djangoproject.com/en/dev/ref/models/querysets/#latest)

StatusUpdate.objects.filter(status_user=user1).latest('status_date_time')


status = StatusUpdate.objects.filter(status_user=user1).order_by('-status_date_time')[1:]

The code above will return the list of statuses. So, when you're calling status.status_id it will never return True.

Try to get first element from the list by adding [0] instead of [1:].


You need to add this to Model for default ordering:

class Meta:
 ordering=['-status_date_time']

And in show_user_homepage write this:

status = StatusUpdate.objects.filter(status_user=user1)[0]
0

精彩评论

暂无评论...
验证码 换一张
取 消