我注意到我的Django應用程序中有一個奇怪的行爲,它運行在apache/mod_wsgi上。有一個屏幕顯示一個表格,基本上是一個下拉列表,其中列出了可用於安排給定網站的列表,根據給定的每週容量(3個網站/周)與已安排的網站總數之間的差異計算周。Django形成不刷新的類
def schedule(request, site_id):
site = Site.objects.get(pk=site_id)
if request.method == 'POST':
form = ScheduleForm(request.POST)
if form.is_valid():
(year, week) = request.POST['available_slots'].split('/')
site.scheduled = week2date(int(year), int(week[1:]))
site.save()
return HttpResponseRedirect('/stats/')
else:
form = ScheduleForm()
return render_to_response('followup/schedule_form.html',{
'form': form,
'site': site
}, context_instance=RequestContext(request))
這裏是形式類(隨訪/ forms.py):
class ScheduleForm(forms.Form):
"""
Temporary lists
"""
schedules = Site.objects.filter(
scheduled__isnull=False
).values('scheduled').annotate(Count('id')).order_by('scheduled')
integration = {}
available_integration = []
# This aggregates all schedules by distinct weeks
for schedule in schedules:
if schedule['scheduled'].strftime('%Y/W%W') in integration.keys():
integration[schedule['scheduled'].strftime('%Y/W%W')] += schedule['id__count']
else:
integration[schedule['scheduled'].strftime('%Y/W%W')] = schedule['id__count']
for w in range(12): # Calculates availability for the next 3 months (3months*4 weeks)
dt = (date.today() + timedelta(weeks=w)).strftime('%Y/W%W')
if dt in integration.keys():
capacity = 3-integration[dt]
else:
capacity = 3
if capacity>0:
available_integration.append([dt, capacity])
"""
Form
"""
available_slots = forms.ChoiceField(
[[slot[0], '%s (%s slots available)' % (slot[0], slot[1])] for slot in available_integration]
)
class IntegrateForm(forms.Form):
integrated_on = forms.DateField(widget=AdminDateWidget())
這
這種形式(ScheduleForm)從下面的視圖(隨訪/ views.py)呈現實際上工作正常,但唯一的問題是,當網站計劃時,可用性列表不會刷新,除非我每次安排網站時重新啓動apache進程。
這就像如果可用性列表將緩存由窗體類...
任何想法將受到熱烈歡迎。提前感謝您提供任何幫助。
塞巴斯蒂安我編輯你的問題,包括你的正確形式。如果您有時間,請刪除您的答案,因爲它應該是您原始問題中的「編輯」。 – rantanplan
@Sebastien請參閱[這裏](http://meta.stackexchange.com/q/155173/155556)爲什麼你的答案沒有被刪除。 – Neal