int()参数必须是字符串,类似字节的对象或数字,而不是“ QueryDict”

时间:2019-12-26 19:48:15

标签: python django

最近,当我单击带有默认用户外键的提交时,我正在尝试将数据保存到数据库中。但是我坚持错误并尝试通过在网上查找来解决,但找不到任何类似的问题。

我的views.py

from django.shortcuts import render,redirect
from .models import TimesheetDetails
from django.contrib.auth.decorators import login_required

# Create your views here.
@login_required(login_url="/login")
def create_timesheet_view(request):
    if request.method=="POST":
        print(request.POST)
        if ('dateToday' and 'dayToday' and 'startTime' and 'endTime' and
            'breakTime' and 'weekType' and 'attendance' and 'normalTime' and 'extraTime'
            and 'holidayTime' and 'workContent') in request.POST:
            #post.user = request.POST.get('user')
            post = TimesheetDetails()
            post.username = request.POST.get('username')
            post.date = request.POST.get('dateToday')
            post.day = request.POST.get('dayToday')
            post.startTime = request.POST.get('startTime')
            post.endTime = request.POST.get('endTime')
            post.breakTime = request.POST.get('breakTime')
            post.normalTime = request.POST.get('normalTime')
            post.overTime = request.POST.get('extraTime')
            post.holidayTime = request.POST.get('holidayTime')
            post.weekType = request.POST.get('weekType')
            post.attendance = request.POST.get('attendance')
            post.content = request.POST.get('workContent')
            post = TimesheetDetails(request.POST,request.FILES)
            print (request.POST)
            instance  = post.save()
            instance.user = request.user
            instance.save()
            return redirect('/list')        
    return render(request,'timesheet/create_timesheet.html')

def view_timesheet(request):
    context = {'timesheet_list': TimesheetDetails.obejcts.all()}
    return render(request,"timesheet/view_timesheet.html")

我的模型。py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class TimesheetDetails(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE,related_name="timesheet",null="False")
    date = models.CharField(max_length = 10)
    day = models.CharField(max_length = 10)
    startTime = models.CharField(max_length =10)
    endTime = models.CharField(max_length =10)
    breakTime = models.CharField(max_length=3)
    normalTime = models.CharField(max_length=10)
    overTime = models.CharField(max_length = 10)
    holidayTime = models.CharField(max_length = 10)
    weekType = models.CharField( max_length = 10)
    attendance = models.CharField( max_length = 10)
    content = models.TextField( max_length = 300)

我的错误

任何帮助将不胜感激。上一次尝试没有外键,我能够保存数据。但是我希望特定用户保存他的数据并仅查看他的数据。

TypeError at /
int() argument must be a string, a bytes-like object or a number, not 'QueryDict'
Request Method: POST
Request URL:    http://127.0.0.1:8000/
Django Version: 2.2.8
Exception Type: TypeError
Exception Value:    
int() argument must be a string, a bytes-like object or a number, not 'QueryDict'
Exception Location: D:\Django\TimeSheetProject\morabu\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 972
Python Executable:  D:\Django\TimeSheetProject\morabu\Scripts\python.exe
Python Version: 3.7.2
Python Path:    
['D:\\Django\\TimeSheetProject',
 'D:\\Django\\TimeSheetProject\\morabu\\Scripts\\python37.zip',
 'D:\\Django\\TimeSheetProject\\morabu\\DLLs',
 'D:\\Django\\TimeSheetProject\\morabu\\lib',
 'D:\\Django\\TimeSheetProject\\morabu\\Scripts',
 'c:\\program files\\python\\Lib',
 'c:\\program files\\python\\DLLs',
 'D:\\Django\\TimeSheetProject\\morabu',
 'D:\\Django\\TimeSheetProject\\morabu\\lib\\site-packages']
Server time:    Fri, 27 Dec 2019 04:35:51 +0900

1 个答案:

答案 0 :(得分:1)

您需要删除post = TimesheetDetails(request.POST,request.FILES)行,因为TimesheetDetails不是Form。话虽这么说,work with forms可能更好。

另一个问题是'foo' and 'bar' in request.POST将仅检查'bar'是否在request.POST中,因为它将仅评估'foo'的真实性,并且非空字符串具有True为真实。

@login_required(login_url="/login")
def create_timesheet_view(request):
    if request.method=="POST":
        print(request.POST)
        if all(x in request.POST for x in ('dateToday', 'dayToday', 'startTime', 'endTime', 'breakTime', 'weekType', 'attendance', 'normalTime', 'extraTime', 'holidayTime', 'workContent')):
            post = TimesheetDetails.objects.create(
                user=request.user,
                date=request.POST.get('dateToday'),
                day=request.POST.get('dayToday'),
                startTime=request.POST.get('startTime'),
                endTime=request.POST.get('endTime'),
                breakTime=request.POST.get('breakTime'),
                normalTime=request.POST.get('normalTime'),
                overTime=request.POST.get('extraTime'),
                holidayTime=request.POST.get('holidayTime'),
                weekType=request.POST.get('weekType'),
                attendance=request.POST.get('attendance'),
                content=request.POST.get('workContent')
            )
            return redirect('/list')        
    return render(request,'timesheet/create_timesheet.html')

但是最好在这里定义一个ModelForm,因为这样可以删除很多样板代码。

此外,您最好在redirect(..)调用中使用视图的 name ,而不是绝对路径。