Django request.user模型自我函数

时间:2014-06-24 19:27:15

标签: python django

我想在我的模型自我功能中获取当前记录的用户。 我试过这个。

class My_model(models.Model):
    user = models.OneToOneField(User)
    image = models.ImageField(upload_to='uploads/',default='uploads/no-img.jpg')
    #other fields
    def show_photo(self,request):
        show_photo = False
        if Photo_request.objects.filter(who=request.user,whose=self.user).exists():
            my_request = Photo_request.objects.get(who=request.user,whose=self.user)
            request_accepted = my_request.accepted            
            if request_accepted:
                show_photo = True                  
        return show_photo
    show_photo = property(show_photo)

在我的模板中

{% for profile in profiles %}  
    {% if profile.show_photo %}  
        <img src="{{MEDIA_URL}}{{ profile.image }}" alt="image" />
    {% endif %}
{% endfor %}

但此功能无效。我试过没有请求参数和自定义ID,这是工作。我的代码有问题吗?

2 个答案:

答案 0 :(得分:1)

编写自定义标记:

程序my_app / templatetags / my_app_tags.py

from django.template import Library

register = Library()

@register.assignment_tag(takes_context=True)
def show_photo(context):
    request = context['request']
    profile = context['profile']
    return profile.show_photo(request) # Instead of passing request I suggest to pass request.user here

通过加载template_tags:

在模板中使用它
{% load my_app_tags %}

{% for profile in profiles %}  
    {% show_photo as show %}
    {% if show %}  
        <img src="{{MEDIA_URL}}{{ profile.image }}" alt="image" />
    {% endif %}
{% endfor %}

答案 1 :(得分:0)

我在current_thread包中使用了threading函数。

  1. utils / request_utiles.py 文件中添加新的中间件,如下所示:

    utils / request_utiles.py

    from threading import current_thread
    from django.utils.deprecation import MiddlewareMixin
    
    _requests = {}
    
    def get_current_request():
        t = current_thread()
        if t not in _requests:
            return None
        return _requests[t]
    
    
    class RequestMiddleware(MiddlewareMixin):
        def process_request(self, request):
            _requests[current_thread()] = request
    
  2. settings.py 文件中添加中间件

    settings.py

    MIDDLEWARE = [
        ...
        'utils.request_utils.RequestMiddleware',
    ]
    
  3. 在任何模型中使用 get_current_request()函数。

    在您的代码中,您可以使用以下代码:

    from utils.request_utils import get_current_request
    
    class My_model(models.Model):
        user = models.OneToOneField(User)
        image = models.ImageField(upload_to='uploads/',default='uploads/no-img.jpg')
        ...
    
        def show_photo(self):
            request = get_current_request() # you can get current request in here.
    
            show_photo = False      
            if Photo_request.objects.filter(who=request.user,whose=self.user).exists():
                my_request = Photo_request.objects.get(who=request.user, whose=self.user)
                request_accepted = my_request.accepted            
                if request_accepted:
                    show_photo = True                  
            return show_photo
    
        show_photo = property(show_photo)