python - Django how to only allow specific users to see a page
问题描述
my models.py
class Utilizador(AbstractBaseUser):
id = models.AutoField(db_column='ID', primary_key=True)
departamentoid = models.ForeignKey(Departamento, models.DO_NOTHING, db_column='DepartamentoID', blank=True, null=True)
email = models.CharField(db_column='Email', max_length=255, blank=True, null=True, unique=True)
nome = models.CharField(db_column='Nome', max_length=255, blank=True, null=True)
class Inscrio(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)
utilizadorid = models.ForeignKey('Utilizador', models.DO_NOTHING, db_column='UtilizadorID')
participanteinfoid = models.ForeignKey('Participanteinfo', models.DO_NOTHING, db_column='ParticipanteinfoID')
my view.py
def ver_col(request, pk):
user = Utilizador.objects.get(id=request.user.id)
insc = Inscrio.objects.get(utilizadorid=request.user.id)
if request.user.is_authenticated:
if request.user.id==insc.utilizadorid.id:
return render(request, 'main/ver_col.html',
{
"insc": insc
})
So, I want users to see this "ver_col.html" but they are only allowed to enter the pages with the UtilizadorID (of the table Inscrio) associated with the user logged in at the moment (request.user.id or request.user).
Basically I want all users to be able to see the template but only their own data
解决方案
不确定您要实现什么,但如果 pk 是用户模型的 pk 那么,
if request.user.id== pk
意味着当前登录的用户只查看自己的页面和一些其他用户的页面。这应该在您对用户进行身份验证之后进行。
推荐阅读
- javascript - 图片右侧无法点击
- android - 在 react-native 应用程序中禁用 Android 主页、返回和最近的导航按钮
- laravel - 使用带有新的 laravel 路由语法的路由操作
- android - 如果英语在 Android 中具有更高的优先级,则应用程序语言回退到默认设置
- android - Android)如何使用系统后退按钮?
- powershell - 使用代码更新 Sharepoint 2013 属性包设置
- airflow - 通用 Airflow 数据暂存运算符
- oracle - 具有自定义返回值的 PL/SQL 包游标(如在过程中)
- r - 在 R 中总结具有多个功能的数据框?
- docker - 使用 Lambda 将代码存储库从 Github 迁移到 AWS CodeCommit