【问题标题】:DJANGO 'WSGIRequest' object has no attribute 'get'DJANGO \'WSGIRequest\' 对象没有属性 \'get\'
【发布时间】:2023-01-05 18:16:46
【问题描述】:

我收到此错误“WSGIRequest”对象在我的代码中没有属性“get” 下面是我在 views.py 中的函数

def user_attendance(request):
   # Get the attendance records for the current user
    attendance_records = Attendance.objects.filter(user=request.user)
 
    # Create a form instance
    form = CompensationRequestForm()

   # Check if the form has been submitted
    if request.method == 'POST':
        # Bind the form with the POST data
        form = CompensationRequestForm(request.POST)
        # Check if the form is valid
        if form.is_valid():
            # Save the form data
            form.save()
            # Redirect to the user_attendance view
           return redirect('user_attendance')
    context = {'attendance_records': attendance_records, 'form': form}

    # Render the template with the attendance records and form
    return render(request, 'user_attendance.html', context)

下面是我在 forms.py 中的表格

class CompensationRequestForm(forms.Form):
    date = forms.DateField()
    reason = forms.CharField(widget=forms.Textarea)
    def save(self):
        # Save the form data to the database
        pass

如何解决这个问题? chatgpt 没有帮助,所以我在这里问

【问题讨论】:

    标签: django request wsgi


    【解决方案1】:

    而不是这个 form = CompensationRequestForm(request.POST) 尝试这种方式:

    form = CompensationRequestForm(data=request.POST)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 2015-04-15
      • 2015-10-19
      • 2021-11-16
      • 2021-11-23
      • 1970-01-01
      • 2021-12-13
      相关资源
      最近更新 更多