【问题标题】:get request.session from a model method in django从 django 中的模型方法获取 request.session
【发布时间】:2010-06-02 10:58:40
【问题描述】:

嘿,是否可以从 django 中的模型方法中获取 request.session 值?

重大更新

这是我的模型

class GameDiscussion(models.Model):
    game = models.ForeignKey(Game)
    message = models.TextField()
    reply_to = models.ForeignKey('self', related_name='replies', null=True, blank=True)
    created_on = models.DateTimeField(blank=True, auto_now_add=True)
    userUpVotes = models.ManyToManyField(User, blank=True, related_name='threadUpVotes')
    userDownVotes = models.ManyToManyField(User, blank=True, related_name='threadDownVotes')
    votes = models.IntegerField()

    def html(self):
        DiscussionTemplate = loader.get_template("inclusions/discussionTemplate")
        return DiscussionTemplate.render(Context({
            'discussion': self,
            'replies': [reply.html() for reply in self.replies.all().order_by('-votes')]
    }))

    def _find_users_who_have_voted(self):
        user_list = []
        for user in self.userDownVotes.all():
            user_list.append(user.id)
        for user in self.userUpVotes.all():
            user_list.append(user.id)   
        return user_list

    users_voted = property(_find_users_who_have_voted)

我的视图是这样调用的

<ul>
    {% for discussion in discussions %}
        {{ discussion.html }}
    {% endfor %}
</ul>

和模板

<li>
<small>
    ({{ discussion.votes }} votes) 

    {% if user_id not in discussion.users_voted %}

      user not in list!

    {% endif %}     

</small>
<strong>{{ discussion.message }}</strong> 
{% if replies %}
    <ul>
        {% for reply in replies %}
            {{ reply }}
        {% endfor %}
    </ul>
{% endif %}

值“user_voted”返回对此讨论投票的用户 ID 列表。

我想看看 request.session['user'].id 值是否在这个列表中

【问题讨论】:

    标签: django model methods


    【解决方案1】:

    为什么不在视图中使用 Django 的 render_to_string,这样可以很高兴地获得请求,并且完全避免使用模型方法?

    更新:浏览完大型更新后,您应该查看 Django 的 inclusion tags 并使用模型中的数据来填充模板,而不是使用模型来呈现模板。保持你的模型和你的模板分开 - Django 是一个 MVT / MCV 框架有充分的理由:)

    【讨论】:

      【解决方案2】:

      您可以使用 threadlocals 中间件访问当前用户以及他们的会话

      http://code.arcs.org.au/gitorious/django/django-andsome/blobs/ee8447e3dad2da9383ff701ec640b44cd50d2b0a/middleware/threadlocals.py

      但请记住:

      http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser

      您的问题可能有更好的解决方案。也许您想详细说明为什么需要模型级别的 request.session?

      更新:

      既然你明确地调用了一些方法 - 可能是从一个视图 - 你为什么不把 request.user 作为参数放到你的 html 方法中?

      models.py:

      def html(self, user):
          your code...
      

      views.py:

      yourmodel.html(request.user)
      

      更新到您的 MEGA UPDATE

      这正是 {% include %} 的用途:

      在你的第一个模板中这样做:

      {% for discussion in discussions %}
          {% include "discussion.html" }}
      {% endfor %}
      

      第二个模板在其命名空间中有 request.user.id:

      {% if request.session.user.id not in discussion.users_voted %}
        user not in list!
      {% endif %}     
      

      【讨论】:

      • 我正在使用模型来解析模板,但我需要将上下文传递给它。但是,当模板被解析时,我无权访问 request.session(我正在将 request.session 值与另一个值进行比较)
      猜你喜欢
      • 2018-07-02
      • 2011-01-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 2018-10-24
      • 2020-09-19
      • 2017-09-14
      相关资源
      最近更新 更多