Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

django - form doesn't submit productbacklogs to database

here is my project code . I after posting data by form nothing happens.

#model.py

from django.db import models
from projectapp.models import Project

class Productbacklog(models.Model):
    project=models.ForeignKey(Project,on_delete=models.CASCADE,default=None)
    pbId=models.IntegerField(primary_key=True)
    pbTitle=models.CharField(max_length=100)
    pbPriority=models.IntegerField(blank=True, null=True)

    class Meta:
        unique_together=('project','pbId')


    def __str__(self):
        return self.pbTitle

#forms.py

from django import forms
from productbacklogapp.models import Productbacklog
from projectapp.models import Project

class ProductbacklogForm(forms.ModelForm):


    class Meta:
          model = Productbacklog
          exclude=('pbId','project')
          fields=['pbTitle']

#views.py

  def productbacklogall(request):
    if request.method == 'POST':
        form = ProductbacklogForm(request.POST)
        if form.is_valid():
            form.instance.manage = Project.objects.get_or_create(cname=form.cleaned_data['manage_id'])
            form.save()
        messages.success(request, ('new productbacklog added'))
        return redirect('productbacklogall')
    else:
         pb_all=Productbacklog.objects.all()
         return render(request, 'productbacklogall.html', {'pb_all':pb_all})

    

I think that issue is on forms.py or views.py but I can't find it. I'm so greatful if anyone can help me.

here is also my html code,when I submit something the method is post but I don't know why it doesn't go to data basse.

#productbacklogall.html

{%extends 'base.html'%}
{%block title%}
<title>backlog all</title>
{%endblock title%}
{%block content%}
    </br>
     {% if messages %}
{% for message in messages %}
    <div class="alert alert-primary" role="alert">
     {{ message }}
   <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
</div>


{% endfor %}
   {% endif %}
<div class="container">

 <form method="POST"  class="row">
     {% csrf_token %}
     <label class="col-lg-4"></label>
     <input type="text" class="form-control" name="Project" placeholder="project title?"/>
      <input type="number" class="form-control" name="pbId" placeholder="backlog id?"/>
      <input type="text" class="form-control" name="pbTitle" placeholder="pb title?"/>









     <button type="submit" class="btn btn-primary col-lg-2">add project</button>
</form>
</div>
    </br>
    </br>


   <table class="table table-bordered text-center">
   <thead class="thead-dark">
   <tr>
       <th scope="col"> backlog-title</th>
       <th scope="col">Edit</th>
       <th scope="col">delivarable_task</th>
       <th scope="col">related project</th>


   </tr>
   </thead>

    <tbody>
    {% load static %}

{% if pb_all %}
{% for obj in pb_all %}

    <tr>
      <td>{{ obj.pbTitle }}</td>
      <td><a href='{% url "delete_productbacklog" obj.pbId %}'>delete</a></td>
      <td> <a href=#> delivarabletask </a></td>
        <td><a href="{% url 'project' %}">{{ obj.project.pbTitle }}</a></td>
    </tr>


    {% endfor %}
    {% endif %}
      </tbody>
</table>


</div>
{%endblock content%}
question from:https://stackoverflow.com/questions/65651289/form-doesnt-submit-productbacklogs-to-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your form data are not valid, to see them, add this code to your views :

else: messages.error(request, "Error")

like this :

  def productbacklogall(request):
    if request.method == 'POST':
        form = ProductbacklogForm(request.POST)
        if form.is_valid():
            form.instance.manage = Project.objects.get_or_create(cname=form.cleaned_data['manage_id'])
            form.save()
            messages.success(request, ('new productbacklog added'))
        else:
            messages.error(request, "Error")
        return redirect('productbacklogall')
    else:
         pb_all=Productbacklog.objects.all()
         return render(request, 'productbacklogall.html', {'pb_all':pb_all})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...