0

I am unable to get the data to my database am I missing something. Followed a couple tutorials, tried my own tweaking but no luck thus far. I m not getting any errors but when I click submit on the form my database is not populating.

My View

def createUser(request):
    # form = UserForm()
    args = {}    

    if request.method=='POST':
        userform = UserForm(request.POST)
        petform = PetForm(request.POST)

        if userform.is_valid() and petform.is_valid():
            newuser = userform.save()
            newpet = petform.save(False)

            newpet.newuser = newuser
            newpet.save()
            return render(request, 'pet_list.html' )
    else:
        userform = UserForm()
        petform = PetForm()

    args.update(csrf(request))
    args['userform'] = userform
    args['petform'] = petform
    return render(request, 'add_user_pet.html', args)

My Template

  <form action="/ " method="POST" >
    {% csrf_token %} 
    <h3 class="details">User Info</h3>
      {{userform}}
      <h3 class="details">Pet Info</h3>
      {{petform}}

  <input type="submit"/>
  </form>

Form.py

class UserForm(forms.ModelForm):
    name = NameField()
    email = forms.CharField(label='E-mail',widget=forms.TextInput(attrs={"placeholder":"Whats your email"}))

    class Meta:
        model = User
        fields = ['name', 'email']

#################################################


class PetForm(forms.ModelForm):

    class Meta:
        model = Pet
        fields = [
            'name', 
            'submitter',
            'species',
            'breed',
            'description',
            'sex',
            'age'
            ]

My Urls.py

urlpatterns = [
    path('', views.home, name='home'),
    path('user_list', views.userlist, name='userlist'),
    path('pet_list', views.petList, name='petlist'),
    path('user_pet', views.userPet, name='userpet'),
    path('add_user_pet', views.createUser, name='add_user_pet')
    path('user_detail/<int:user_id>', views.userdetail, name='detail'),
1
  • 1
    Share your urls.py. Commented May 26, 2021 at 5:26

1 Answer 1

1

Your form action points to "/" which is going to home view but you want to call "add_user_pet" to direct your POST to createUser view.

 <form action="/ " method="POST" >

should be

 <form action="add_user_pet" method="POST" >

the more "django" way would be to not put the url directly but use the name of the path as you defined it in urlspatterns:

 <form action="{% url 'add_user_pet' %}" method="POST" >

in this way you are using the urlspatterns name.

Sign up to request clarification or add additional context in comments.

5 Comments

Tried this but still not populating database.
1) do you see the POST request in your server terminal? 2) do you get the pet_list.html rendered? 3) what is the result of your is_valid() checks .... I would debug with prints to see what happens step by step
Trying this approach....will let you know the outcome
I can now see the requested data in my terminal and the pet_list is now being rendered. But for some reason form aint valid....
thank you. It now works.......in my views file I changed newpet.newuser to newpet.user. as 'user;' is the foregn key in my Pet model. Also fixed some tab spaces. That along with your input solved it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.