django models request get id error Room matching query does not exist
我有两个模型,当我请求时。post.get("房间ID")或("ID")我得到的错误房间匹配查询不存在。如何解决这个问题?帮助我
1 2 3 4 5 6 7 8 9 | class Room(models.Model): status = models.BooleanField('Status',default=True) name = models.CharField('Name', max_length=100, unique=True) class Book(models.Model): date = models.DateTimeField('Created',auto_now_add=True) from_date = models.DateField('Check-in') to_date = models.DateField('Check-out') room = models.ForeignKey(Room, related_name='booking') |
我需要详细的房间申请获取ID,预订日期范围(从日期到日期)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | def room_detail(request,pk): room = get_object_or_404(Room,pk=pk) if request.method == 'POST': form = BookForm(request.POST,room=room) if form.is_valid(): s = form.save(commit=True) s.save() return redirect(request.path) else: form = BookForm() #roomid = Room.objects.values('id') type = request.POST.get('id') # or get('room_id') rooms = Room.objects.get(id=type) start_dates = rooms.booking.values_list('from_date',flat=True) end_dates = rooms.booking.values_list('to_date',flat=True) dates = [start + timedelta(days=i) for start, end in zip(start_dates,end_dates) for i in range((end-start).days+1)] c = {} c['form'] = form return render_to_response('rooms_detail.html',c) |
请帮帮我,提前谢谢
您正在查看request.post,即使request.method不等于"post"。这将不起作用,因为当请求不是HTTP POST时,请求的POST成员为空。
移动你的台词
1 2 3 | #roomid = Room.objects.values('id') type = request.POST.get('id') # or get('room_id') rooms = Room.objects.get(id=type) |
在post方法中。
1 2 3 4 5 6 7 8 9 | if request.method == 'POST': form = BookForm(request.POST,room=room) if form.is_valid(): #roomid = Room.objects.values('id') type = request.POST.get('id') # or get('room_id') rooms = Room.objects.get(id=type) s = form.save(commit=True) s.save() return redirect(request.path) |