对于给定的插槽,用户选择(应该有时间,俱乐部,日期和与之相关的法院),如何将“username”中存储的值保存到Opening类中的“预订”字段?
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)
答案 0 :(得分: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()
您还需要检查是否允许用户更新该开头。