i want to create an application that admin can make user with a new messages that encrypted into database. I have the core function and all can work properly. But, i still can't encrypt the message from text to md5/sha1. I have read and try this and this. But, i still can't do it. I'm new in django, and I'm very grateful for your response. Thank you
This is my models:
class UserProfile(models.Model):
user = models.OneToOneField(User) #digunakan untuk relasi ke model User (default) alias UserProfile adalah sebagai extending model
CATEGORY_CHOICES = (
('admin','Admin'),
('user','User'),
)
hak_akses = models.CharField(max_length=100, choices = CATEGORY_CHOICES)
messages = models.CharField(max_length=100, blank=True)
# password_pckelas = models.CharField(max_length=100, blank=True)
# Override the __unicode__() method to return out something meaningful!
def __unicode__(self):
return self.user.username
This is my view function:
def tambah_user(request, template_name='form_user.html'):
#cek session
if 'username' in request.session and request.session['hak_akses'] == 'admin':
#ambil dari database untuk mengaktifkan ubah_password_admin
users = User.objects.all()
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)
messages = profile_form.data['messages'] #ambil messages
if request.method == 'POST':
if user_form.is_valid() and profile_form.is_valid():
#i want hash messages in here and then save into database
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
return redirect('manajemen_user')
else:
print user_form.errors, profile_form.errors
else:
user_form = UserForm()
profile_form = UserProfileForm()
data = {
'user_form': user_form,
'profile_form': profile_form,
'object_list': users
}
return render(request, template_name, data)
#jika session tidak ada
else:
return HttpResponseRedirect('/simofa/logout')
This is my html templates:
<form name="tambah_user" class="form-horizontal style-form" method="POST">{% csrf_token %}
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Nama User</label>
<div class="col-sm-10">
<!-- <input type="text" class='form-control'> -->
{{ user_form.username }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Password</label>
<div class="col-sm-10">
{{ user_form.password }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Messages</label>
<div class="col-sm-10">
{{ profile_form.messages }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Hak Akses</label>
<div class="col-sm-10">
{{ profile_form.hak_akses }}
<br><br><br>
<button type="submit" class="btn btn-theme">Submit</button>
</div>
</div>
</form>