开发者

Creating unique URL/address for a resource to share - Best practices

开发者 https://www.devze.com 2023-02-03 20:40 出处:网络
In my application there is a need to create unique URLs (one per resource) that can be shared. Something like Google Calendar Private address for a calendar. I want to know what are the best practices

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

If it helps my application is in Django.

Please let me know if this question开发者_Go百科 needs more explanation.


This should be very straightforward. In your urls.py file you want a url like this:

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

Then you handle this in views.py with a function called:

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

Finally, you get to use this in your templates too, using naming:

{% url priv-resource string %}

Just ensure that in your models.py:

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

I might even be tempted to use a signal handler to generate this field automatically upon save of the object. See the documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消