为资源共享创建唯一的URL /地址 - 最佳做法 [英] Creating unique URL/address for a resource to share - Best practices

查看:162
本文介绍了为资源共享创建唯一的URL /地址 - 最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,需要创建可以共享的唯一URL(每个资源一个)。像Google日历的某个日历的私人地址。我想知道这是最好的做法。

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.

如果它有助于我的应用程序在Django。

If it helps my application is in Django.

请让我知道这个问题需要更多的解释。

Please let me know if this question needs more explanation.

推荐答案

这应该很简单。在您的urls.py文件中,您需要一个这样的URL:

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"),

然后在 views.py 中使用一个函数处理这个问题:

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 %}

只需确保在您的models.py:

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.

这篇关于为资源共享创建唯一的URL /地址 - 最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆