Creating a Unique Resource Sharing URL - Best Practices

In my application, there is a need to create unique URLs (one per resource) that can be shared. Something like Google Calendar. A private address for a calendar. I want to know what are the best methods for doing this.

If this helps my application in Django.

Please let me know if this question needs a more detailed explanation.

+4
source share
1 answer

It should be very simple. In your urls.py file you need the url:

url(r'/resource/(?P<resource_name>\w+)', 'app.views.resource_func', name="priv-resource"), 

Then you process this in views.py with the function:

 def resource_func(request, resource_name): # look up resource based on unique string resource_name... 

Finally, you can also use this in your templates using names:

 {% url priv-resource string %} 

Just make sure in your models.py:

 class ResourceModel(models.Model) resource_name = models.CharField(max_size=somelimit, unique=True) 

I may be tempted to use a signal handler to create this field automatically after saving the object. See the documentation.

+3
source

Source: https://habr.com/ru/post/1334848/


All Articles