2

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

模型.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)

视图.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 %}

感谢您对此的帮助!

4

2 回答 2

1

视图.py:

def listEzMap(request):
    user_list = ezMap.objects.filter(user=request.user)
    return render_to_response("map_list.html", {'user_list': user_list})
于 2013-02-01T15:29:42.330 回答
1

您始终可以从 request as 访问已登录的用户user = request.user,然后使用该用户信息根据用户做出决定:

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

您应该检查评论中提到的django 身份验证dm03514系统。

于 2013-02-01T17:15:32.723 回答