我在这里做错了什么。我已经尝试了好几个小时了。我认为我在Django get_context_data函数方面遇到问题。
错误为“ PatientBedAllotmentList”对象没有属性“对象”
views.py
@method_decorator(login_required, name='dispatch')
class PatientBedAllotmentList(ListView):
model = BedAllotment
template_name = 'room/bed_allotment_list.html'
def get_context_data(self, **kwargs):
context = super(PatientBedAllotmentList, self).get_context_data(**kwargs)
start = BedAllotment.objects.get(id=self.object.id).allotment_date
end = BedAllotment.objects.get(id=self.object.id).departure_date
amount = BedCreate.objects.get(id=self.object.id).cost
days_number = abs((end - start).days)
days_number = int(days_number)
amount_due = amount * days_number
context['account_type'] = AccountUser.objects.get(user_id=self.request.user.id)
hospital_id = AccountUser.objects.get(user_id=self.request.user.id).hospital_id
allotment_details = BedAllotment.objects.filter(hospital_id=hospital_id)
context['allotment'] = allotment_details
context['amount'] = amount_due
return context
urls.py
from django.conf.urls import url
from medisaver.room import views
urlpatterns = [
url(r'^room-category/create/$', views.RoomCategoryCreate.as_view(), name='room_category_create'),
url(r'^room-category/list/$', views.RoomCategoryList.as_view(), name='room_category_list'),
url(r'^room-category/update/(?P<hospital_id>[0-9A-Fa-f-]+)/(?P<category_id>[0-9]+)/$', views.RoomCategoryUpdate.as_view(), name='room_category_update'),
url(r'^room-category/delete/(?P<hospital_id>[0-9A-Fa-f-]+)/(?P<category_id>[0-9]+)/$', views.RoomCategoryDelete.as_view(), name='room_category_delete'),
url(r'^hospital-rooms/list/$', views.RoomList.as_view(), name='room_list'),
url(r'^hospital-rooms/create/$', views.RoomCreateView.as_view(), name='room_create'),
url(r'^hospital-rooms/update/(?P<category_id>[0-9]+)/(?P<room_id>[0-9]+)/$', views.RoomUpdate.as_view(), name='room_update'),
url(r'^hospital-rooms/delete/(?P<category_id>[0-9]+)/(?P<room_id>[0-9]+)/$', views.RoomDelete.as_view(), name='room_delete'),
url(r'^hospital-rooms/beds/create/$', views.BedCreateView.as_view(), name='bed_create'),
url(r'^hospital-rooms/beds/list/$', views.BedList.as_view(), name='bed_list'),
url(r'^hospital-rooms/beds/update/(?P<room_id>[0-9]+)/(?P<bed_id>[0-9]+)/$', views.BedUpdate.as_view(), name='bed_update'),
url(r'^hospital-rooms/beds/delete/(?P<room_id>[0-9]+)/(?P<bed_id>[0-9]+)/$', views.BedDelete.as_view(), name='bed_delete'),
url(r'^hospital-rooms/beds/patient-bed-allotment/$', views.BedAllotmentCreate.as_view(), name='bed_allotment'),
url(r'^hospital/discharge-patient/(?P<allotment_id>[0-9]+)/(?P<patient_id>[0-9A-Fa-f-]+)/$', views.BedDischarge.as_view(), name='patient_bed_discharge'),
url(r'^hospital/bed-allotment-list/$', views.PatientBedAllotmentList.as_view(), name='patient_bed_list'),
]
答案 0 :(得分:0)
self.object在ListView中没有任何意义。
如果要获取stay_time,则可以在模型中进行操作,并使用@notebook = Notebook.new(notebooks_params)
@notebook.user = current_user #Assuming you have this method available
在模板中访问,甚至可以通过乘以计算模板中的金额。但我什至可以在detailview中做到。在该模型中创建一个方法,例如
object.stay_time
答案 1 :(得分:0)
我能够通过调用form_valid
函数来修复它。我在视图中使用了BedDischarge
类上的函数。并且能够使用self.object
。然后在我的模板中进行模板调用。
@method_decorator(login_required, name='dispatch')
class BedDischarge(UpdateView):
model = BedAllotment
template_name = 'room/bed_discharge.html'
form_class = BedAllotmentUpdateForm
pk_url_kwarg = 'allotment_id'
success_url = reverse_lazy('patient_bed_list')
def get_context_data(self, **kwargs):
context = super(BedDischarge, self).get_context_data(**kwargs)
context['account_type'] = AccountUser.objects.get(user_id=self.request.user.id)
return context
def form_valid(self, form):
get_allot_id = self.object.id
bed_allot = BedAllotment.objects.get(id=get_allot_id)
start_time = bed_allot.allotment_date
end_time = form.instance.departure_date
cost = BedCreate.objects.get(id=self.object.bed_id).cost
days_spent = (end_time - start_time).days
amount = cost * days_spent
form.instance.days = days_spent
form.instance.amount = amount
return super(BedDischarge, self).form_valid(form)