1

对于用户选择的给定插槽(应该有时间、俱乐部、日期和球场),我如何将存储在“用户名”中的值保存到“开放”类的“预订”字段中?

def vote(request, club_id):
    if 'username' in request.POST and request.POST['username'] and 'slot' in request.POST and request.POST['slot']:
        username = request.POST['username']
        slot = request.POST['slot']
        #save the value stored in "username" to the "reservation" field in class Opening with the other associated data (club, time, day, court)
        return HttpResponseRedirect('reserve/templates/booked.html',{'username':username, 'slot':slot})
    else:
        return render_to_response('reserve/templates/vote.html', {'error': True})

Avail_times.html:

<form action="/clubs/{{ club.id }}/vote/" method="post">
{% csrf_token %}
{{ today }}<br>
{% for slot in open_slots %}
    <input type="checkbox" name="slot" id="slot{{ forloop.counter }}" value="{{ slot.id }}" />
    <label for="slot{{ forloop.counter }}">{{ slot.slot }} on Court {{slot.court}}</label><br />
{% endfor %}

<br>
{{ tomorrow }}<br>
{% for slot in tom_open_slots %}
    <input type="checkbox" name="slot" id="slot{{ forloop.counter }}" value="{{ slot.id }}" />
    <label for="slot{{ forloop.counter }}">{{ slot.slot }} on Court {{slot.court}}</label><br />
{% endfor %}    
<input type="text" name="username" />
<input type="submit" value="Reserve" />

模型:

from django.db import models
import datetime

class Club(models.Model):
    establishment = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    def __unicode__(self):
        return self.establishment

class Court(models.Model):
    club = models.ForeignKey(Club)
    day = models.ForeignKey(Day)
    court = models.IntegerField(max_length=200)
    def __unicode__(self):
        return unicode(self.court)

class Opening(models.Model):
    club = models.ForeignKey(Club)
    day = models.DateField('date')
    court = models.ForeignKey(Court)
    slot = models.TimeField('slot')
    reservation = models.CharField(max_length=200)
    def __unicode__(self):
        return unicode(self.slot)
4

1 回答 1

0

如果要更新Opening模型,则需要知道是哪一个。

将 写入opening_id表格。我不确定你的插槽和开口是如何相关的,所以你必须弄清楚这一点。

我通常会使用get_object_or_404来获取相关的Opening

username = request.POST['username']
opening = get_object_or_404(Opening, id=request.POST['opening_id'])
opening.reservation = username
opening.save()

您还需要检查是否允许用户更新该开口。

于 2012-05-23T07:26:31.883 回答