限制在utils.py上的视图-Django
我使用tempalte使用Django创建日历,但是现在我坚持将本日历上的事件的视图限制在创建该事件的用户中。
我没有这个日历的模型,仅用于事件,而日历当前基于我的util.py文件:
utils.pys.py
class Calendar(HTMLCalendar):
def __init__(self, year=None, month=None):
self.year = year
self.month = month
super(Calendar, self).__init__()
# formats a day as a td
# filter events by day
def formatday(self, day, events):
contracts_starting_per_day = events.filter(starting_date__day=day)
contracts_ending_per_day = events.filter(ending_date__day=day)
contracts_paying_per_day = events.filter(payment_date__day=day)
d = ''
for contract in contracts_starting_per_day:
if contract.company_client:
client = contract.company_client
elif contract.person_client:
client = contract.person_client
else:
client = '-'
d += f"<a href='http://127.0.0.1:8000/contracts/editct/{contract.id}'> <li class='calendar-li starting' title='{contract.contract_name}'> {contract.contract_name} <p class='calendar-p'> {client} </p> </li> </a>"
if day != 0:
return f"<td class='calendar-td'><span class='date'>{day}</span><ul class='calendar-ul'> {d} </ul></td>"
return "<td class='calendar-td'></td>"
# formats a week as a tr
def formatweek(self, theweek, events):
week = ''
for d, weekday in theweek:
week += self.formatday(d, events)
return f"<tr class='calendar-tr'> {week} </tr>"
# formats a month as a table
# filter events by year and month
def formatmonth(self, withyear=True):
contracts = Contract.objects.filter(starting_date__year=self.year, starting_date__month=self.month)
cal = f'<table border="0" cellpadding="0" cellspacing="0" class="calendar">\n'
cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n'
cal += f'{self.formatweekheader()}\n'
for week in self.monthdays2calendar(self.year, self.month):
cal += f'{self.formatweek(week, contracts)}\n'
return cal
views.pys.py
class CalendarView(LoginRequiredMixin, generic.ListView):
model = Contract
template_name = 'calendar.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Get the actual month to display the calendar
d = get_date(self.request.GET.get('month', None))
agenda = Calendar(d.year, d.month)
html_agenda = agenda.formatmonth(withyear=True)
#context['calendar'] = context['calendar'].filter(user=self.request.user)
context['calendar'] = mark_safe(html_agenda)
context['prev_month'] = prev_month(d)
context['next_month'] = next_month(d)
return context
def get_date(req_day):
if req_day:
year, month = (int(x) for x in req_day.split('-'))
return date(year, month, day=1)
return datetime.today()
def prev_month(d):
first = d.replace(day=1)
prev_month = first - timedelta(days=1)
month = 'month=' + str(prev_month.year) + '-' + str(prev_month.month)
return month
def next_month(d):
days_in_month = calendar.monthrange(d.year, d.month)[1]
last = d.replace(day=days_in_month)
next_month = last + timedelta(days=1)
month = 'month=' + str(next_month.year) + '-' + str(next_month.month)
return month
我已经尝试过#context ['calendar'] = context ['calendar']。滤波器(user = self.request.user)
在我的视图上,但是我去了这个错误:
keyError在/议程/日历'日历'
是否只有在用户记录和创建事件的用户相等的情况下,才有任何选项可以恢复日历上的事件?
我也尝试过:
for contract in contracts_starting_per_day:
if contract.user == request.user:
在我的日历util上,但是由于此util没有收到任何请求,因此无法使用它。
I've used a tempalte to create a calendar using Django, but now i'm stuck to limit the view of the events on this calendar to just the user who have created the event.
I have no model for this calendar, just for the events, and the calendar is currently based on my util.py file:
utils.py
class Calendar(HTMLCalendar):
def __init__(self, year=None, month=None):
self.year = year
self.month = month
super(Calendar, self).__init__()
# formats a day as a td
# filter events by day
def formatday(self, day, events):
contracts_starting_per_day = events.filter(starting_date__day=day)
contracts_ending_per_day = events.filter(ending_date__day=day)
contracts_paying_per_day = events.filter(payment_date__day=day)
d = ''
for contract in contracts_starting_per_day:
if contract.company_client:
client = contract.company_client
elif contract.person_client:
client = contract.person_client
else:
client = '-'
d += f"<a href='http://127.0.0.1:8000/contracts/editct/{contract.id}'> <li class='calendar-li starting' title='{contract.contract_name}'> {contract.contract_name} <p class='calendar-p'> {client} </p> </li> </a>"
if day != 0:
return f"<td class='calendar-td'><span class='date'>{day}</span><ul class='calendar-ul'> {d} </ul></td>"
return "<td class='calendar-td'></td>"
# formats a week as a tr
def formatweek(self, theweek, events):
week = ''
for d, weekday in theweek:
week += self.formatday(d, events)
return f"<tr class='calendar-tr'> {week} </tr>"
# formats a month as a table
# filter events by year and month
def formatmonth(self, withyear=True):
contracts = Contract.objects.filter(starting_date__year=self.year, starting_date__month=self.month)
cal = f'<table border="0" cellpadding="0" cellspacing="0" class="calendar">\n'
cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n'
cal += f'{self.formatweekheader()}\n'
for week in self.monthdays2calendar(self.year, self.month):
cal += f'{self.formatweek(week, contracts)}\n'
return cal
views.py
class CalendarView(LoginRequiredMixin, generic.ListView):
model = Contract
template_name = 'calendar.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Get the actual month to display the calendar
d = get_date(self.request.GET.get('month', None))
agenda = Calendar(d.year, d.month)
html_agenda = agenda.formatmonth(withyear=True)
#context['calendar'] = context['calendar'].filter(user=self.request.user)
context['calendar'] = mark_safe(html_agenda)
context['prev_month'] = prev_month(d)
context['next_month'] = next_month(d)
return context
def get_date(req_day):
if req_day:
year, month = (int(x) for x in req_day.split('-'))
return date(year, month, day=1)
return datetime.today()
def prev_month(d):
first = d.replace(day=1)
prev_month = first - timedelta(days=1)
month = 'month=' + str(prev_month.year) + '-' + str(prev_month.month)
return month
def next_month(d):
days_in_month = calendar.monthrange(d.year, d.month)[1]
last = d.replace(day=days_in_month)
next_month = last + timedelta(days=1)
month = 'month=' + str(next_month.year) + '-' + str(next_month.month)
return month
I've tried #context['calendar'] = context['calendar'].filter(user=self.request.user)
on my views, but i went trough this error:
KeyError at /agenda/calendar 'calendar'
Is there any option to restric the events on the calendar only if the user logged and the user who created the events are equal?
I've also tryed to:
for contract in contracts_starting_per_day:
if contract.user == request.user:
On my Calendar util, but since this util does not receive any request, it's not possible to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试从上下文中从上下文中检索
日历
。错误是在此行引起的:您应该在
get_queryset
视图方法中为模型定义QuerySet。该问题将在您在context_object_name
中设置的变量中的上下文中访问:然后在模板中:
You are trying to retrieve
calendar
from context while it is not yet in the context. Error is caused in this line:You should define the queryset for your model in
get_queryset
method of View. That queryset will be accessible in context in variable you set incontext_object_name
:Then in template: