简体   繁体   中英

Django request.user in model self function

I want to get current logged user in my model self function. I tried this.

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)

In my template

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

but this function not working. I tried without request parameter and custom id, thats working. Is there any problem in my code?

Write the custom tag:

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

Use it in template by loading the 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 %}

I used current_thread function in threading package.

  1. Add a new middleware in utils/request_utiles.py file as following:

    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. Add the middleware in settings.py file

    settings.py

     MIDDLEWARE = [ ... 'utils.request_utils.RequestMiddleware', ]
  3. Use get_current_request() function in any models.

    In your code, you can use as following:

     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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM