0

I want to convert text to sha1 in django. But, i'm not find the way how to do it if field attribut wrapped by the form.

This is my views:

def ubah_password_email(request, pk):
    #cek session
    if 'username' in request.session and request.session['hak_akses'] == 'user':
        user = get_object_or_404(User, pk=pk) #ambil id dengan get
        profile = UserProfile.objects.filter(user=user).first() 
        email_form = EmailForm(data=request.POST, instance=profile) #gunakan instance untuk mengambil data yang sudah ada
        users = User.objects.all()
        if request.POST:
            if email_form.is_valid():
                email = email_form.save(commit=False)
                email.save()
                return redirect('home')
        else:
            email_form = EmailForm(instance=profile)

        data = {
                'email_form': email_form,
                'object_list': users,
        }
        return render(request, 'ubah_password_email.html', data)
    else:
        return HttpResponseRedirect('/simofa/logout')

This is my model

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) 
    password_email = 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 forms

class EmailForm(forms.ModelForm):
    password_email = forms.CharField(widget=forms.PasswordInput())
    class Meta:
        model = UserProfile
        fields = ('password_email',)

i'm trying using this and this reference. But, i still can't convert text to sha1?

I'm very grateful for your input. So, please help me :)

Community
  • 1
  • 1
ranggatry
  • 125
  • 1
  • 15
  • 1
    No. Do not do this. Do not ever do this. Django has a very good built-in method of setting and hashing passwords which has been thoroughly reviewed by security experts, and even then they keep finding (and fixing) issues. Please read and follow the [authentication](https://docs.djangoproject.com/en/1.7/topics/auth/) docs, especially the section on [custom user models](https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#substituting-a-custom-user-model) if that's what you really need to do. – Daniel Roseman Mar 11 '15 at 10:13
  • @DanielRoseman is correct - please follow his advice. From the looks of your code, you're trying to allow people to create other users. Django has built-in forms and views for doing these operations - use them. – Brandon Taylor Mar 11 '15 at 10:22
  • I'm just curious about this technique. This is not for authentication. My code just to store password information for future email will be used to authenticate the email in other applications. That's it, not for authentication deffault because I have my own authentication. So, I need to know how to do a hash of the text to sha1 / md5 – ranggatry Mar 12 '15 at 01:55

1 Answers1

0

I'm not sure why you want or need a second password field. But make_password allows you to generate a hashed password:

from django.contrib.auth.hashers import make_password

Docs: https://docs.djangoproject.com/en/1.7/topics/auth/passwords/#django.contrib.auth.hashers.make_password

Source: https://github.com/django/django/blob/master/django/contrib/auth/hashers.py#L58

mishbah
  • 5,487
  • 5
  • 25
  • 35