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

吃可爱长大的小学妹 提交于 2019-12-04 11:19:21

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.

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