@login_required'if'语句中的装饰器?

时间:2019-02-13 21:07:06

标签: django python-3.x django-views django-authentication

我有一个模型“ Playlist”,其属性“ private”可以为True或False(因此为private或public)。我只想在'private = False'时使用@login_required装饰器,但是无法弄清楚如何实现此结果。这是我的models.py文件:

class Playlist(models.Model):
    """Allow a user to create a customized list of songs."""
    name = models.CharField(max_length=100)
    image = models.ImageField(upload_to='playlists/%Y/%m/%d', blank=True, null=True)
    songs = models.ManyToManyField('Song')
    description = models.TextField(blank=True, null=True, max_length=1000)
    date_added = models.DateTimeField(auto_now_add=True)
    private = models.BooleanField()

    def __str__(self):
        """String for representing the model object."""
        return self.name

    def get_absolute_url(self):
        """Returns the url to access a detail record for this song."""
        return reverse('playlist-detail', args=[str(self.id)])

还有我的views.py文件:

def playlist(request, playlist_id):
    """Show a single playlist and associated data."""
    playlist = Playlist.objects.get(id=playlist_id)
    songs = playlist.songs.all()
    for song in songs:
        if song.url:
            song.video_id = song.url.replace("https://www.youtube.com/watch?v=", "")

    context = {'playlist': playlist, "playlist_songs": songs}

    return render(request, 'great_songs_app/playlist.html', context)

我偶然发现了一个类似的线程,但问题没有答案:Conditionally apply login_required decorator in Django

我想象代码看起来像OP所发布的内容,其视图功能之前是:

if playlist.private == True:
    @login_required
    ...

但是显然那是行不通的。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您无需尝试应用@login_required,而只需让视图保持未修饰状态,然后执行以下操作,检查用户是否已通过身份验证:

from django.shortcuts import redirect
from django.conf import settings

def playlist(request, playlist_id):
    """Show a single playlist and associated data if user is authenticated."""
    playlist = Playlist.objects.get(id=playlist_id)
    if not playlist.private or (playlist.private and request.user.is_authenticated):
        songs = playlist.songs.all()
        for song in songs:
            if song.url:
                song.video_id = song.url.replace("https://www.youtube.com/watch?v=", "")

        context = {'playlist': playlist, "playlist_songs": songs}

        return render(request, 'great_songs_app/playlist.html', context)
    else:
        redirect(settings.LOGIN_URL)