Django - 用户会话显示用户数据

时间:2013-02-01 15:13:07

标签: django django-models django-views

我想显示user.username foreignKey是会话用户名的条目列表。我想显示与会话用户相关的特定数据。

models.py:

from django.db import models

class User(models.Model):
    username = models.CharField(max_length=10)
    email = models.EmailField(verbose_name='e-mail')
    date_signed = models.DateTimeField('date signed')

class ezMap(models.Model):
    map_name = models.CharField(max_length=50, )
    user = models.ForeignKey(User)

views.py:

from django.shortcuts import *
from ezmapping.models import *

def listEzMap(request):
    #really not sure how to do it...
    username = request.session['username']
    user_list = ezMap.objects.get(map_name = username)
    return render_to_response("map_list.html", {'user_list': user_list})

map_list.html模板:

{% extends "base.html" %}
{% block content %}
<div class="list">
  <h2> Map list </h2>
    {% for object in user_list %}
<li>{{ object.map_name }}</li>
{% endfor %}
</div>
{% endblock %}

感谢您对此的帮助!

2 个答案:

答案 0 :(得分:1)

views.py:

def listEzMap(request):
    user_list = ezMap.objects.filter(user=request.user)
    return render_to_response("map_list.html", {'user_list': user_list})

答案 1 :(得分:1)

您始终可以通过user = request.user从请求中访问已登录的用户,然后使用该用户信息根据用户做出决策:

user = request.user
user_list = ezMap.objects.get(map_name = user.username)

您应该在评论中检查dm03514提到的django authtication系统。