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

有些话、适合烂在心里 提交于 2019-12-21 05:17:15

问题


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 needs more explanation.


回答1:


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.



来源:https://stackoverflow.com/questions/4641866/creating-unique-url-address-for-a-resource-to-share-best-practices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!