Задать вопрос

Тел: +7 965 3737 888

450

Просмотров

0

Ответов

RequestFetchingMixin

<p>More a proof of concept than anything, this mixin addresses a perceived "shortcoming" of django forms: the inability to interact with the current request, and specifically the "current user".
</p>
<p>Usage:
</p>
class SomeForm(forms.Form, RequestFetchingMixin):
  # fields...

  def __init__(self, *args, **kwargs):
    if self.request:
      # interact with self.request?
    super(SomeForm, self).__init__(*args, **kwargs)
    if self.request:
      # more interaction

  def clean(self):
    if self.request:
      # blah blah self.request.user blah
<p>Notes:
</p>
<ul><li>
     This reaches "into the past" to pull an existing HttpRequest object, regardless of what name it was bound to, into "the present".
 </li>

 <li>
     If multiple requests are found (what on earth are you doing??), the one bound to "request" (or the first one found, if that's not available) will be used.
 </li>

 <li>
     By default it goes up to 5 frames back before giving up, but that can obviously be changed if you think you need it.
 </li>

 <li>
     This won't work everywhere.  self.request is guaranteed to be present, but may be None.
 </li>

 <li>
     Just because you have a self.request doesn't mean self.request.user will be a valid user.  This is subject to all the rules that a regular view's request is subject to (because it <em>is</em> a regular view's request!)
 </li>

 <li>
     This isn't magic; it's just python.
 </li>
</ul>

Вопрос полезен? Да0/Нет0
file_4071.py(958.0б)
None