Home>

Hi, explain why an error occurs.

error

TracEBack (MOST Recent Call Last):
  File "/library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    Response= Get_Response (Request)
  File "/Library/Frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", Line 128, In _Get_Response
    Response= Self.Process_Exception_BY_MIDDleWare (E, Request)
  File "/Library/Frameworks/python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    Response= Wrapped_callback (Request, * callback_args, ** callback_kwargs)
  File "/users/sergey/documents/mysite/django.testsite/testsite/login/views.py", Line 38, in Register
    newuser_form= signupform (Data= Request.post, Instance= Request.user)
  File "/Library/Frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/django/contrib/auth/forms.py", Line 91, In __init__
    Super () .__ init __ (* Args, ** kwargs)
  File "/Library/Frameworks/python.framework/Versions/3.6/lib/python3.6/site-packages/django/forms/models.py", line 291, in __init__
    object_data= model_to_dict (Instance, Opts.fields, Opts.Exclude)
  File "/Library/Frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/django/forms/models.py", line 82, in model_to_dict
    Opts= instance._meta.
  File "/Library/Frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", Line 216, in Inner
    Return Func (Self._Wrapped, * Args)
ATTRIBUTEERROR: 'Anonymoususer' Object Has No Attribute '_Meta'

views

Def Register (Request):
args= {}
Args ['Forms']= SignUpform ()
Args ['Form1']= imagefieldform ()
If Request.post:
    newuser_form= signupform (Data= Request.post, Instance= Request.user)
    image_field_form= imagefieldform (Data= Request.post, instance= request.user.profile)
    ifuser_form.is_valid () and image_field_form.is_valid ():
        user= newuser_form.save (Commit= False)
        user.is_active= false
        user.save ()
        Print ('User Saved')
        image_field_form.save ()
        #Unerfield= Request.post.get ('Username', '')
        Current_Site= Get_Current_Site (Request)
        Mail_Subject= 'Welcome to Site'
        Message= Render_To_String ('login_app /please_active_email.html', {
            'user': user,
            'Domain': Current_Site.Domain,
            'UID': URLSAFE_BASE64_ENCODE (Force_Bytes (user.pk)). Decode (),
            'Token': Account_activation_Token.Make_Token (User),
        })
        to_Email= newuser_form.cleaned_data.get ('username')
        email= emailmessage (
            Mail_Subject, Message, To= [To_Email])
        email.send ()
        Return HttpResponse ('Please Check Your Email')
    ELSE:
        Args ['Forms']= newuser_form
Return Render (Recuest, 'login_app /registration.html', args)

Models

Class Profile (Models.Model):
User= Models.Onetoonefield (User, On_delete= Models.cascade)
email_confirmed= models.Booleanfield (default= false)
avatar= models.imagefield (upload_to= 'images /users', blank= false)
@Receiver (Post_save, Sender= User)
Def Update_user_Profile (Sender, Instance, Created, ** kwargs):
   IF Created:
       Profile.Objects.create (User= Instance)
   instance.profile.save ()

Forms

Class SignupForm (UsercreationForm):
username= Forms.charfield (max_length= 50, required= true, help_text= 'Please enter your email', widget= forms.textinput (attrs= {'posteholder': '[email protected]'}), validators= [REGEXVALIDATOR (Regex= R '([a -z0-9 _-] + \.) * [a -z0-9 _-] + @ [a -z0-9 _-] + (\. [a -z0-9 _-] + ) * \. [az] {2,6} ', message=' incorrect email ')])
first_name= Forms.charfield (max_length= 30, required= true, validators= [REGEXVALIDATOR (REGEX= '[A-Z]', Message= 'IncorRect Name')]
Class Meta:
    Model= user.
    Fields= ('username', 'first_name', 'password1', 'password2',)

Class ImageFieldForm (Modelform):

Class Meta:
    Model= Profile
    Fields= ('avatar',)
  • Answer # 1

    Check

    If Request.user.is_authenticated:
    

    Before your code

    newuser_form= signupform (data= request.post, instance= request.user)