简体   繁体   English

如何在Django 1.7中打印哈希并将其存储在数据库中?

[英]How do you print a hash in Django 1.7 and store it in a database?

I created a hash that I am going to use later for confirmation email logic like this: 我创建了一个哈希,稍后将用它来确认电子邮件逻辑,如下所示:

import hashlib, datetime, random 

def hash(request):
    username = 'johndoe' #inserted line to simplify the code
    random_str = str(random.random()).encode('utf-8')
    salt = hashlib.sha1(random_str).hexdigest()[:5]
    salted = (salt + username).encode('utf-8')
    activation_key = hashlib.sha1(salted).hexdigest()
    return render_to_response('ftest/display.html', activation_key)

My first question is how do I print this in the html so that I can see it when the html renders? 我的第一个问题是如何在html中打印此内容,以便在html渲染时可以看到它?

This doesn't seem to work in my display.html : 这似乎在我的display.html中不起作用:

<p> This is the activation key {{activation_key}} </p>

Next, how would I define this hash field in the database? 接下来,我将如何在数据库中定义此哈希字段? Would it just be a charfield like: 会是像这样的charfield吗?

hash = models.CharField(max_length=200)

render_to_response expects a dict : render_to_response需要一个字典:

return render_to_response('ftest/display.html', {'activation_key': activation_key})

A CharField is the right way to go. CharField是正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM