Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django @在';如果';陈述_Django_Python 3.x_Django Views_Django Authentication - Fatal编程技术网

Django @在';如果';陈述

Django @在';如果';陈述,django,python-3.x,django-views,django-authentication,Django,Python 3.x,Django Views,Django Authentication,我有一个带有属性“private”的模型“Playlist”,该属性可以是真的,也可以是假的(因此也可以是私有的或公共的)。我只想在“private=False”的情况下使用@login\u required装饰器,但我不知道如何实现这个结果。这是我的models.py文件: class Playlist(models.Model): """Allow a user to create a customized list of songs.""" name = models.Ch

我有一个带有属性“private”的模型“Playlist”,该属性可以是真的,也可以是假的(因此也可以是私有的或公共的)。我只想在“private=False”的情况下使用@login\u 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)])
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)
和my views.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)])
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)
我偶然发现了一条类似的线索,但问题没有答案:

我想象代码看起来与OP发布的类似,视图函数前面有:

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

但很明显,这是行不通的。有什么想法吗?

与尝试应用
@login\u 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)