skip to Main Content

can someone help me with my code? Why is it not working?

My views.py

def register(request):
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            fullname = form.cleaned_data.get('fullname')
            emailaddress = form.cleaned_data.get('emailaddress')
            password = form.cleaned_data.get('password')
            form.save()
            return redirect('/')
    else:
        form = RegisterForm()
    return render(request, 'register.html', {'form': form})

forms.py

class RegisterForm(forms.Form):
    fullname = forms.CharField(label='fullname', max_length=100)
    emailaddress = forms.EmailField(label='emailaddress', max_length=150)
    password = forms.CharField(label='password', max_length=100)

Thanks a lot!

Can’t seem to connect the inputed data into the db…

2

Answers


  1. If you wish to save data to database by using Django Forms then its better to use ModelForm. Instead of inheriting the class from forms.Form use ModelForm for example

     class ArticleForm(ModelForm):
       class Meta:
          model = < model class >
          fields = ['required fields']
    

    change according to your use case. for more information check the documentation django modelform

    If you wish to implement a custom user authentication model (both registration and login) use bellow link to know more.
    extending user model

    Login or Signup to reply.
  2. You can try another method also

      userdetails=modelname.objects.create(fullname=fullname,
      emailaddress=emailaddress)
      userdetails.set_password(request.POST['password'])
      userdetails.save()
    

    If you want to use same method, share your error message

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search