如何在django中创建可共享的链接包[关闭]

问题描述 投票:0回答:1

示例1:https://www.freelancer.com/jp/profile(any应用网址-具有登录身份验证的网址

上述链接需要创建可共享的链接,例如:https://www.freelancer.com/h45n64n56nm56

当我在上面共享给公众的链接时,公众可以单击该URL并查看共享页面而无需登录

需要为上述过程创建一个程序包,例如将具有登录身份验证的URL转换为可共享的URL

python django
1个回答
0
投票

在您的应用中,您有urls.py,创建另一个shareable_links.py形式的表单>>

urlpatterns = [ # here goes the links

]

# models.py
imports...


class ShareableLink(models.Model):
    extension = models.CharField(max_length=10) # not url field, we will store only the 
    # extension of the url .. www.domain.com/extension/

在您的应用中views.py

from .shareable_links import urlpatterns as shared_urls
from django.urls import path
from .models import ShareableLink
from django.http.response import HttpResponse
from django.contrib.sites.shortcuts import get_current_site


def create_shareable_link(request):
    link = ShareableLink.objects.create(link=create_random_string())
    shared_urls.append(path(f'{link.extension}', the_view_handler_you_will_create ))
    return HttpResponse(link.url) # for illustration

def create_random_string():
    return .... # implement it as you like, search for uuid in python

这应该使您上路,当然,这需要大量改进并使用CBV,但我只是在说明。

© www.soinside.com 2019 - 2024. All rights reserved.