简体   繁体   English

Django form.is_valid()每次都返回false

[英]Django form.is_valid() is returning false everytime

I have created a django application. 我已经创建了一个django应用程序。 In that, when I click on update button on index.html page, it has to redirect to a webpage showing the form with 3 fields,(title,user, body). 在这种情况下,当我单击index.html页面上的更新按钮时,它必须重定向到一个显示具有3个字段(标题,用户,正文)的表单的网页。 Everytime I click on update, the form is not validating. 每次我单击更新时,该表单都不会生效。 It is always returning false. 它总是返回false。 Actually if we press on the update button, the user field should show the present user's id. 实际上,如果我们按下更新按钮,则用户字段应显示当前用户的ID。 But it is showing default value. 但是它显示默认值。 The body field is becoming empty every time. 身体字段每次都变为空。

forms.py: forms.py:

class ImmediateForm(forms.ModelForm):
title = forms.CharField(widget=forms.TextInput(attrs={'readonly': 'readonly'}),initial="Immediate",max_length=20)
class Meta:
    model = Immediate
    fields = ['title','user','body',]
    widgets = {
        'user': TextInput(attrs={'readonly': 'readonly'})
    }

Models.py: Models.py:

class Immediate(models.Model):
title = models.CharField(default="Immediate",max_length=30)
user = models.ForeignKey(User, default=1,on_delete=models.CASCADE)
body = models.TextField(default="")

def __str__(self):
    return self.title

views.py: views.py:

def update_immediate(request,pk):
if not request.user.is_authenticated:
    return render(request, 'set_goals/index.html')
else:
    print(pk)
    try:
        if request.method=='POST' :
            print('Inside if')
        immediate = Immediate.objects.get(user_id=pk)
        form = ImmediateForm(request.POST or None, instance=immediate or None)
        if form.is_valid():
            print("no")
            immediate = form.save(commit=False)
            immediate.user = request.user.username
            immediate.body = form.cleaned_data['body']
            immediate.save()
            return render(request, 'set_goals/detail_immediate.html', {'immediate': immediate})
        context = {
            "form": form,
        }
        return render(request, 'set_goals/create_template.html', context)
    except:
        if request.method=='POST' :
            print('Inside if')
        form = ImmediateForm(request.POST or None)
        if form.is_valid():
            print("yes")
            immediate = form.save(commit=False)
            immediate.user = request.user
            immediate.body = form.cleaned_data['body']
            immediate.save()
            return render(request, 'set_goals/detail_immediate.html', {'immediate': immediate})
        context = {
            "form": form,
        }
        return render(request, 'set_goals/create_template.html', context)

And the html page where the button is: 按钮所在的html页面:

index.html: index.html的:

    <div class="col-md-6">
          <div class="card mb-4">
              <div class="card-header">
    Immediate
  </div>
            <img class="card-img-top" style="width:495px;height:400px;" src="" alt="Card image cap">
            <div class="card-body">
              <h2 class="card-title">Empty for now</h2>
              <p class="card-text"></p>
                <form class="form-horizontal" role="form" action="" method="POST" enctype="multipart/form-data">
                <a href="{% url 'set_goals:update_immediate' user.id %}" class="btn btn-primary">Update &rarr;</a>
              <a href="{% url 'set_goals:detail_immediate' user.id %}" class="btn btn-primary">Read More &rarr;</a>
              </form>
                </div>

</div>
            </div>

What is the mistake here? 这是什么错误? Why ImmediateForm() is not working? 为什么InstantForm()不起作用? what should be done to correct this? 应该怎么做才能纠正这个问题?

To debug this you can use form.errors before form.is_valid or immediately after. 要调试此错误,可以在form.is_valid之前或之后立即使用form.errors。 Then just output to the console and you look, in what a problem. 然后只是输出到控制台,您会发现问题出在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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