您能帮我解决Django表单提交中的405错误吗?

在基于类的视图中提交表单时遇到问题。我在详细信息视图中使用FormMixin,提交表单时出现405错误。我已经尝试过砍断和更改视图中的代码,但是似乎没有任何效果。

楷模

class Bid(models.Model):
 author = models.ForeignKey(User, on_delete=models.CASCADE)
 tender = models.ForeignKey(Tender, on_delete=models.CASCADE, null=True)
 title = models.CharField(max_length=100)
 specification = models.TextField()
 timeline = models.CharField(max_length=100)
 date_posted = models.DateTimeField(default=timezone.now)
 price = models.IntegerField()

 def __str__(self):
    return self.title

 def get_absolute_url(self):
      return reverse('bid-detail', kwargs={'pk': self.pk})

观看次数

class TenderDetailView(FormMixin, LoginRequiredMixin, DetailView):
  model = Tender
  template_name = 'it_me/tender_detail.html'
  form_class = BidForm

def get_success_url(self):
    return reverse('tender-detail', kwargs={'pk': self.object.id})

def get_context_data(self, **kwargs):
    context = super(TenderDetailView, self).get_context_data(**kwargs)
    context['form'] = BidForm(initial={'post': self.object})
    return context

def post(self, request, *args, **kwargs):
    self.object = self.get_object()
    form = self.get_form()
    if form.is_valid():
        return self.form_valid(form)
    else:
        return self.form_invalid(form)

def form_valid(self, form):
    form.save()
    return super(TenderDetailView, self).form_valid(form)

形式

class BidForm(forms.ModelForm):

    class Meta:
        model = Bid
        fields = ('title', 'specification', 'timeline', 'price')

范本

 <div class="content-section">
  <form method="POST">
    {% csrf_token %}
    <fieldset class="form-group">
        {{ form|crispy }}
    </fieldset>
    <div class="form-group">
        <button class="btn btn-outline-info" type="submit"> Post Bid </button>
    </div>
    </form>
    </div>

    {% for bid in tender.bids.all %}
        <div class=" col-md-12 comment">
            <div class="date">{{ bid.date_posted|date:"F d, Y" }}</div>
            <strong>{{ bid.author }}</strong>
        </div>
    {% empty %}
        <p>No Bids Yet </p>
    {% endfor %}

网址

path('tender/<int:pk>/', TenderDetailView.as_view(), name='tender-detail')

真的很感谢任何帮助,因为我感到沮丧。

评论