@login_required 'if' 语句中的装饰器?
@login_required decorator within an 'if' statement?
我有一个模型 'Playlist',其属性 'private' 可以是 True 或 False(因此是私有的或 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
...
但这显然行不通。有什么想法吗?
而不是尝试应用 @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)
我有一个模型 'Playlist',其属性 'private' 可以是 True 或 False(因此是私有的或 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
...
但这显然行不通。有什么想法吗?
而不是尝试应用 @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)