我有一个日历,假设它连接到当前的admins-association。管理员在日历中选择日期并需要填写表单。 Association-dropdown-list不会显示数据库中的任何数据。我只希望它显示当前管理员关联的关联名称。
我有什么遗失的吗?
还是一个新手,非常感谢你的帮助,伙计们!
models.py
class Administrator(AbstractUser):
...
association = models.ForeignKey(Association)
class Event(models.Model):
...
association = models.ForeignKey(Association)
class Association(models.Model):
asoc_name = models.CharField(max_length=50, null=True, blank=True)
views.py
def event_add_edit(request):
if request.method == 'POST':
form = CalForm(request.user, request.POST)
res = {'success': False}
if paramMissing(request.POST, 'name', res) \
or paramMissing(request.POST, 'location', res) \
or paramMissing(request.POST, 'start', res) \
or paramMissing(request.POST, 'end', res) \
or paramMissing(request.POST, 'allday', res) \
or paramMissing(request.POST, 'description', res) \
or paramMissing(request.POST, 'action', res) \
or paramMissing(request.POST, 'association', res) \
or paramMissing(request.POST, 'synced', res):
return JsonResponse(res)
action = request.POST['action']
name = request.POST['name']
location = request.POST['location']
start = request.POST['start']
end = request.POST['end']
allday = request.POST['allday'] == 'true'
description = request.POST['description']
synced = request.POST['synced'] == 'true'
asoc_id = form.request.POST['association']
association = Association.objects.get(pk=asoc_id.pk)
if action == 'add':
Event.objects.create(
name=name,
location=location,
start=start,
end=end,
allday=allday,
description=description,
synced=synced,
association=asoc
)
res['success'] = True
res['message'] = 'added'
eid = Event.objects.latest('id').id
res['eid'] = eid
res['data'] = Event.objects.values().get(id=eid)
elif action == 'edit':
if paramMissing(request.POST, 'eid', res):
return JsonResponse(res)
eid = request.POST['eid']
event = Event.objects.get(id=eid)
event.name = name
event.location = location
event.start = start
event.end = end
event.allday = allday
event.description = description
event.synced = synced
event.save()
res['success'] = True
res['message'] = 'edited'
res['eid'] = eid
res['data'] = Event.objects.values().get(id=eid)
return JsonResponse(res)
else:
raise Http404
calendar.js
$formwrappers: $(".form-wrapper"),
$formclose: $(".form-close"),
$addEventForm: $("#add-event-form"),
$addEventAction: $("#add-event-action"),
$addEventEid: $("#add-event-eid"),
$addEventSynced: $("#add-event-synced"),
$addEventTop: $("#add-event-top"),
$addEventName: $("#add-event-name"),
$addEventLocation: $("#add-event-location"),
$addEventSdate: $("#add-event-sdate"),
$addEventStime: $("#add-event-stime"),
$addEventEdate: $("#add-event-edate"),
$addEventEtime: $("#add-event-etime"),
$addEventAllday: $("#add-event-allday"),
$addEventDesc: $("#add-event-desc"),
$addEventAsoc: $("#add-event-asoc"),
showAddForm: function () {
cal.$addEventAction.val("add");
cal.$addEventSynced.val("false");
cal.$addEventName.val("");
cal.$addEventLocation.val("");
cal.$addEventAllday[0].checked = false;
cal.$addEventDesc.val("");
var association = $(this).val("association");
alert (association);
$.ajax ({
type: "GET",
url: '/calendar/',
data: association,
success: function(data) {
cal.$addEventAsoc.val(data);
},
error: function (error_data) {
console.log("error");
console.log(error_data)
}
});
var date = new Date($(this).data("date"));
cal.$addEventTop.text(date.toDateString());
date.setHours(cal.currDate.getHours());
date.setMinutes(cal.currDate.getMinutes());
cal.$addEventSdate.val(date.toDateInput());
cal.$addEventStime.val(date.toTimeInput());
date.shiftHour(1);
cal.$addEventEdate.val(date.toDateInput());
cal.$addEventEtime.val(date.toTimeInput());
cal.$addEventForm.addClass("visible");
}
calendar.html
<div class="form-wrapper" id="add-event-form">
<form action="" method="POST" role="form">
{% csrf_token %}
<input type="hidden" name="action" id="add-event-action" value="add">
<input type="hidden" name="eid" id="add-event-eid" value="">
<input type="hidden" name="synced" id="add-event-synced" value="false">
<ul>
<li class="form-li">
<span class="form-top" id="add-event-top"></span>
<span class="btn-fa btn-close form-close">
<i class="fa fa-times"></i>
</span>
</li>
<li class="form-li">
<span class="input-field">
<input id="add-event-name" maxlength="50" name="name" placeholder="Event Name" type="text" required>
</span>
</li>
<li class="form-li">
<span class="input-field">
<input id="add-event-location" maxlength="100" name="location" placeholder="Location" required>
</span>
</li>
<li class="form-li">
<span class="input-name">Starts</span>
<span class="input-field datetime-field">
<input id="add-event-sdate" name="start_date" type="date" ><input id="add-event-stime" name="start_time" type="time" required>
</span>
</li>
<li class="form-li">
<span class="input-name">Ends</span>
<span class="input-field datetime-field">
<input id="add-event-edate" name="end_date" type="date" ><input id="add-event-etime" name="end_time" type="time" required>
</span>
</li>
<li class="form-li">
<label for="add-event-asoc" class="input-name"> Association</label>
<select id="add-event-asoc" name="association">
<option value="{{ association.id }}">{{ association.asoc_name }} </option>
</select>
</li>
<li class="form-li">
<input id="add-event-allday" type="checkbox" name="allday">
<label for="add-event-allday" class="input-name"> All Day</label>
</li>
<li class="form-li">
<textarea id="add-event-desc" name="description" placeholder="Description" maxlength="200"></textarea>
</li>
<li class="form-li">
<input class="form-btn form-close" type="button" value="Cancel">
<input class="form-btn btn-right" type="submit" value="Save">
</li>
</ul>
</form>
</div>
forms.py
class CalForm(forms.ModelForm):
association = forms.ModelChoiceField(queryset=Association.objects.none(),
widget=forms.Select(attrs={'class': 'form-control'}),
required=True)
class Meta:
model = Event
exclude = ['last_login', 'date_joined']
fields = ['association', ]
def __init__(self, user, *args, **kwargs):
super(CalForm, self).__init__(*args, **kwargs)
self.fields['association'].queryset = Association.objects.filter(asoc_name=user.association)
urls.py
urlpatterns = [
url(r'^calendar/event/get/(?P<start>.+)/(?P<end>.+)/', views.event_get),
url(r'^calendar/event/delete/', views.event_delete),
url(r'^calendar/event/addedit/', views.event_add_edit),
url(r'^calendar/', views.calendar.as_view(), name='calendar'),
]