Home > Enterprise >  getting error in user_profile form submition
getting error in user_profile form submition

Time:07-20

I am getting error user_profile() got an unexpected keyword argument 'phoneNumber' Using the post method and trying to post data to the database but getting this error.

model.py

class user_profile(models.Model):
 user=models.ForeignKey(User, on_delete=models.CASCADE)
 phoneNumber=models.CharField( max_length=12, blank=True)
 name=models.CharField(max_length=100, blank=True)
 address=models.CharField(max_length=500,blank=True)

user_profile.html

 {% extends 'firmApp/basic.html'%}
 {% block body%}
 <div >
 <div >
    <form action="/user_profile/" method="post" >{% csrf_token %}
        <div >
          <label for="name" >Full-Name</label>
          <input type="text"  name="Name" >
          <div id="emailHelp" >We'll never share your email with anyone else.</div>
        </div>
        <div >
          <label for="address" >Address</label>
          <input type="text"  name="address">
        </div>

        <div >
            <label for="phoneNumber" >Contact Number</label>
            <input type="text"  name="phoneNumber">
          </div>

        <div >
            <label for="formFile" >select image</label>
            <input  type="file" name="formFile">
          </div>
    
        
        <button type="submit" >Submit</button>
    </form>
</div>
{%endblock%}

views.py

def  user_profile(request):
if request.method=="POST":
    phoneNumber=request.POST.get('phoneNumber')
    name=request.POST.get('Name')
    address=request.POST.get('address')
    user=request.user
    print(user)
    profile=user_profile(phoneNumber=phoneNumber, name=name, address=address,user=user)
    profile.save()
return render(request, "firmApp/blog/user_profile.html")     

I have checked the complete code but am unable to solve the issue. I am getting below error

user_profile() got an unexpected keyword argument 'phoneNomber'

CodePudding user response:

You have a naming clash - both your view and your model are called user_profile. When you try to create the model in your view, python is actually calling the view instead of the model. The view function doesn't expect to receive the phoneNumber argument, hence the error. You just need to change one of the names to something distinct.

CodePudding user response:

That's because your view and model have the same name and since you're calling user_profile from inside the view, it's calling the wrong user_profile. You should rename one of them in order to have distinct names and solve your error.

According to the conventions, class names should be written like UserProfile.

  • Related