0

my template has three columns in one form where they work separately. If I give input for contact column and male column it will save in my database. So basically I want to update the male only as contacts,male and female inputs are present in same html form. I can update the contact but male or female inputs. They all are in same page.I want to make them working separately so I can update them.

The problem is that when I give inputs for the it gets posted but not for male and female inputs. Even when I include the contact1 of male class in forms.py,the form does not get submitted.


from django.urls import path , include
from  . import views



urlpatterns = [
    path('', views.index, name='index'),
    path('info/', views.info, name='info'),
    path('features/', views.features, name='features'),
    path('add/', views.add, name='add'),
    path('add/' ,views.success, name='success'),
    path('update/<str:pk_test>/' ,views.update, name='update')
   

]

views.py

def update(request,pk_test):
    template_name = 'update.html' 
  
 
    # if request.method =='POST':
    #     c_form = commentForm(request.POST,instance=contacts)
    #     if c_form.is_valid() :
             
    #         contact = c_form.save()     
    #         messages.success(request, "Contact Updated")
    #         return redirect("success")     
    #     else:     
    #         c_form = commentForm()            
    # context = {
      
    #     'c_form' : c_form,
       

    # }

    contact = Contact.objects.get(id=pk_test) 
     
    c_form = commentForm(instance=contact)
    m_form = maleForm(instance=contact)  
    f_form = femaleForm(instance=contact)
    if request.method =='POST':
        c_form = commentForm(request.POST,instance=contact)
        m_form = maleForm(request.POST,instance=contact)
        f_form = femaleForm(request.POST,instance=contact)
        if c_form.is_valid() and m_form.is_valid():
                gender = c_form.cleaned_data.get('gender')
                username= c_form.cleaned_data.get('name')
                c_form.save()  
               
                m_form.save()                 
                
             
                       
                messages.success(request, f"Form Submitted: {username}")
                return redirect("success")     
        else:     
                    c_form = commentForm()
                    m_form = okForm()   
                    f_form = femaleForm()
                    
       
              
    context = {
      
        'c_form' : c_form,
        'm_form' : m_form,
        'f_form' : f_form,
        'contact' : contact,

    }

    return render(request , template_name , context)
    ```

models.py




class Male(models.Model):
    contact1 = models.ForeignKey(Contact,  on_delete=models.CASCADE,null=True)
    chest = models.CharField(max_length=30 , blank=True)
    neck = models.CharField(max_length=30 , blank=True)
    full_shoulder_width = models.CharField(max_length=30 , blank=True)
    right_sleeve = models.CharField(max_length=30 , blank=True)
    left_sleeve = models.CharField(max_length=30 , blank=True)
    bicep = models.CharField(max_length=30 , blank=True)
    def __str__(self):
        return chest

 


add.py

def add(request):
    template_name = 'add.html'
    c_form = commentForm()
    m_form = maleForm()   
    f_form = femaleForm()
                    

   
    contact = Contact.objects.all()  
    if request.method =='POST':
        c_form = commentForm(request.POST)
        m_form = maleForm(request.POST)
        f_form = femaleForm(request.POST)
        if c_form.is_valid() and (m_form.is_valid() or f_form.is_valid()):
                gender = c_form.cleaned_data.get('gender')
                username= c_form.cleaned_data.get('name')
                contact = c_form.save()     
                
                if gender == 'female':
                       
                        female = f_form.save(commit=False)
                        female.contact2 = contact
                        female.save()
                        messages.success(request, f"Form Submitted: {username}")
                        return redirect("success")
                else:
                        male = m_form.save(commit=False)
                        male.contact1 = contact
                        male.save()
                       
                        messages.success(request, f"Form Submitted: {username}")
                        return redirect("success")     
        else:     
                    c_form = commentForm()
                    m_form = maleForm()   

1 Answer 1

1
Hay there k, 
Since You are working with Django , there is a built in Function called (UpdateView), 

Before Please check this Project that i did a while ago , it can help you a lot : The Project -- Go to Posts , inside the project tree

Here i will drop a quick example on how that works :

class Update_Posts(UpdateView):
    form_class = Form_Change
    model = MyPosts
    template_name = 'form_page.html'

    # Define a post method
    def post(self, request, pk):
        if self.request.user.admin_user or self.request.user.staff_user or self.request.user.active_user:
            instance = get_object_or_404(MyPosts, pk=pk)
            form = self.form_class(
                self.request.POST, self.request.FILES or None, instance=instance)
            if form.is_valid():
                return self.form_valid(form)
            else:
                print('this data isn\'t Valid')
Sign up to request clarification or add additional context in comments.

2 Comments

Im praticing in function based view.So I want to do in that not in class based view for now
Fair enough. That's even better to practice basics first , Good luck with that

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.