Python 创建唯一的哈希/加密 url

标签 python django hash sha1

我想创建一个应用程序,如果有人想申请为我的公司工作,我将在我的应用程序中创建一个条目,然后向他们发送一个唯一的 URL,该 URL 指向我想要的表单页面要进行哈希处理的 URL,但我不太确定如何执行此操作。下面是我的代码:

View .py:

def applicant_email_view(request):
    form = ApplicantEmailForm(request.POST or None)
    if form.is_valid():
        inst = form.save(commit=False)
        subject = 'Applicant Form'
        text_content = 'Hola'
        html_content = """
                    <h3 style="color: #0b9ac4><a>http://127.0.0.1:8080/hiring/application_form/</a></h3>
                    """
        to = [inst.applicant_email]
        send_email(subject, text_content, html_content, to)

        inst.save()
    return render(request, 'hiring/applicant_email.html', {'form': form})

def application_form_view(request):
    form = JobApplicationForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        inst = form.save(commit=False)
        inst.save()
    context = {'form': form}
    return render(request, 'hiring/application_form.html', context=context)


def send_email(subject, text_content, html_content, to):
    from_email = 'test@testing.studio'
    footer = """<h5>Do not reply to this email. This is automated email notification from LemonCORE.</h5>
                </br>
                """
    email_body = html_content + footer
    msg = EmailMultiAlternatives(subject, text_content, from_email, to)
    msg.attach_alternative(email_body, "text/html")
    msg.send()

url.py:

url(r'^hiring/application_email/$', applicant_email_view, name='application_email_view'),
    url(r'^hiring/application_form/$', application_form_view, name='application_form_view'),

最佳答案

我假设您的主要想法是使用唯一的字符串作为标识符,并且您正在尝试使用哈希作为唯一的字符串。

如果是这样,我建议使用 uuid模块。

做类似的事情

import uuid
unique_key = str(uuid.uuid4()).replace('-')
url = 'http://127.0.0.1:8080/hiring/application_form/{0}/'.format(unique_key)
# Store the unique_key in the DB with the user object,
# and use it in the email..etc.,

在 URL 中获取映射捕获 UUID

url(r'^hiring/application_form/(?P<uuid>[a-f0-9]+)/$', applicant_form_view, name='application_form_view')

然后根据需要在 View 和进程中使用它。

关于Python 创建唯一的哈希/加密 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48903221/

相关文章:

python - PyQt 中的布局由 QWidgetItems 而不是 QPushButtons 组成,并且没有属性文本

python - 从 django 渲染中显示 html 中的字典

python - 在散列冲突中,CPython 如何知道哪个值存储在索引 HASHVALUE 以及哪个值存储在 RESOLUTIONINDEX

python - 交叉比较列表中数百万个哈希值的最有效方法

python - Caffe Net 不训练(训练时损失不会改变)

python - 如何使用正则表达式来匹配年份和月份?

python - 表单不保存对象重定向到新表单 View

django - 当不使用 TLS/SSL 时,如何在 Django 中删除 HttpRequest 连接?

c# - 如何将这个序列转换成这个散列?

python - 如何从pandas中的csv文件中进行计数和百分比