0

Can someone help me with code for forms.py views.py and urls? views.py

def PasswordRecover(request):
    if request.method=='POST':
        form=UserGet(request.POST)
        user= form.save(commit=False)
        if(user.is_active):
            return redirect('password_reset/security',pk=user.pk)
    else:
        form = UserGet()

    return render(request, 'passwordreset.html', {'form':form})

def Security(request):
    return HttpResponse('what do we add here??')

forms.py:

class UserGet(forms.ModelForm):
    class Meta:
        model = CustomUser
        fields = ('username',)

urls.py:

from django.urls import path
from . import views

app_name='users'
urlpatterns = [
    path('signup/', views.SignUp.as_view(), name='signup'),
    path('password_reset',views.PasswordRecover,name='password_recovery'),
    path('password_reset/security',views.Security,name='SecurityRecover'),
]

Error I am getting: Exception Value:The CustomUser could not be created because the data didn't validate.

custom user Model:

class CustomUser(AbstractUser):
    # First/last name is not a global-friendly pattern
    name = models.CharField(blank=True, max_length=255)
    MALE = 'M'
    FEMALE = 'F'
    GENDER_CHOICES=(
        (MALE,'Male'),
        (FEMALE,'Female'),
    )
    gender = models.CharField(max_length=1,choices=GENDER_CHOICES,default=MALE)
    bio = models.CharField(max_length=255,blank=True)
    DOG = 'DOG'
    PHONE = 'PHONE'
    USER_QUESTIONS = (
        (DOG, 'Dog'),
        (PHONE, 'Phone'),
    )
    user_questions = models.CharField(max_length=255,choices=USER_QUESTIONS, default=DOG)
    answer = models.CharField(max_length=255,blank=True)


    def __str__(self):
        return self.email

Signup view:

class SignUp(generic.CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'signup.html'

forms.py:

from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm, UsernameField
from .models import CustomUser



class CustomUserCreationForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = CustomUser
        fields = ('username', 'email', 'gender', 'user_questions', 'answer')


class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = UserChangeForm.Meta.fields

This are my files. I don't know how do I proceed further

manjy
  • 109
  • 1
  • 2
  • 12
  • In this question you have two problems: 1- is **The CustomUser could not be created because the data didn't validate**. and this is totaly normal because your custom user lacks some stuff, like `USERNAME_FIELD` and the `AUTH_USER_MODEL` in settings file... and 2- use the user question if the user forgot the password. I suggest to split it up to two questions one for each problem. – Youssef BH Jun 24 '18 at 09:50

1 Answers1

0

Answer for your error :

Before saving [user= form.save(commit=False)] the form you must validate the form.

Something like this :

if form.is_valid():
    user= form.save(commit=False)
else:
    render(request, 'passwordreset.html', {'form':form})

Suggestion regarding your security question query:

I think you can have two fields in CustomUser model, one for security question (Maybe based on options ?) and another one for user answer. Then based on your logic you can implement it in view. Or let me know your logic if you need my help there also.

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
  • ok, so what I am doing is while user signs up he chooses one question as the security question and writes an answer for the Question. Now when he presses forgot the password, he will get a page where he enters his username and then he goes to another page where I can show him his security question, but how do I achieve this? how do I access users info in that other page? Can you help? – manjy Jun 22 '18 at 08:44
  • when user submits his/her username you get that in the view you can use that username , right ? – Umair Mohammad Jun 22 '18 at 09:34
  • yes, but I don't know how do I pass pk value from one page to another. I am trying to figure that out. – manjy Jun 24 '18 at 08:49
  • What do you mean by "pass pf value from one page to another" in term of djangi? – Umair Mohammad Jun 25 '18 at 18:41