Enzo Massaki
Enzo Massaki

Reputation: 311

How can I encrypt <int:pk> inside my URLs?

I think it's a dumb question, but I can't solve this problem anyway. I'm building a simple card game with chatrooms in Django. When a mod creates a room, to enter this room you need to use the following URL:

cardgame/room/<int:pk> 

where inside of <int: pk> is replaced by the id of the room created. My problem is that some random user could enter the room of id=x just using a link like cardgame/room/x without being invited. I wanted to encrypt the id number whenever a room is created, just like when you create a Google meet call but I dont know how to this using Django/Python.

How can I do this?

Upvotes: 0

Views: 202

Answers (2)

sudden_appearance
sudden_appearance

Reputation: 2197

Possible duplicate of Using a UUID as a primary key...

import uuid
from django.db import models

class MyUUIDModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

This changes the default behaviour of django models, which are creating id as an AutoField. Either you can do this or add an additional uuid field

Upvotes: 1

BrendanPJ
BrendanPJ

Reputation: 161

What about adding a UUID field to your model to create a universally unique identifiers and then using that as the path variable instead?

Something like this:

class Room(models.Model):
    unique_id = models.UUIDField(default=uuid.uuid4, unique=True)

Upvotes: 0

Related Questions