简体   繁体   中英

Django - Cannot update the form

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()   
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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM