亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據ForeignKey返回結果

根據ForeignKey返回結果

神不在的星期二 2023-04-18 15:24:34
幾天來我的代碼一直有問題。我聲稱對 Django 沒有經驗。我想根據特定的“CalendarGroups”獲得結果,例如僅特定 CalendarGroups 的所有日歷。要在其上過濾日歷的組的 ID 我想通過 url whit“goup_id”傳遞它。在 Url.py 中:urlpatterns = [    url(r'^$', views.hello, name='hello'), #Prova    url(r'^home/$', views.GroupCalendarView.as_view(), name='home'), #Prova    url(r'^home/(?P<group_id>\d+)/$', views.CalendarsOfGroupView.as_view(), name='calendar_view'),]在模型.py 中:class CalendarGroups(models.Model):    GRP = (       ('Amazon', 'Amazon'),       ('VICHY', 'VICHY'),    )    name = models.CharField(max_length = 155, blank=True, null=True)        @property    def get_html_url(self):        url = reverse('', args=(self.id,))        return f'<a href="{url}"> {self.name} </a>'   class Calendar(models.Model):    name = models.CharField(max_length=200)    #created_by    group = models.ForeignKey(CalendarGroups, on_delete = models.CASCADE, default='')     @property    def get_html_url(self):        url = reverse('cal:calendar_view', args=(self.id,))        return f'<a href="{url}"> {self.name} </a>'在 views.py 中:class GroupCalendarView(generic.ListView):    model = CalendarGroups    template_name = 'cal/home.html'    def get_context_data(self, **kwargs):        context = super().get_context_data(**kwargs)        #print (context)        return contextclass CalendarsOfGroupView(generic.ListView):        model = Calendar    template_name = 'cal/calendarOfGroup.html'    def get_queryset(self, **kwargs):        context = super().get_context_data(**kwargs)                       return context這是我當前的代碼,我不知道如何使用我在 views.py 中通過“group_id”傳遞的內容。
查看完整描述

2 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

class CalendarsOfGroupView(generic.ListView): 

      model = Calendar 

      template_name = 'cal/calendarOfGroup.html' 


      def get_context_data(self, **kwargs): 

          context = super().get_context_data(**kwargs) 

          group_id = self.kwargs['group_id']

          # create var to hold all objects that do not have name==group_id

          object_list = Calendar.objects.exclude(name=group_id)

          # update the context

          context.update({'object_list': object_list})          

          return context

編輯:過濾日歷對象以僅包含匹配的對象group_id


class CalendarsOfGroupView(generic.ListView): 

      model = Calendar 

      template_name = 'cal/calendarOfGroup.html' 


      def get_context_data(self, **kwargs): 

          context = super().get_context_data(**kwargs) 

          group_id = self.kwargs['group_id']

          # create var to hold all objects that do have name==group_id

          object_list = Calendar.objects.filter(name=group_id)

          # update the context

          context.update({'object_list': object_list})          

          return context


查看完整回答
反對 回復 2023-04-18
?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

group_id或任何其他 URL 參數將在以下位置可用kwargs

group_id = self.kwargs['group_id']

get_queryset您可以在您的方法或您的類可用的任何其他方法中訪問它。



查看完整回答
反對 回復 2023-04-18
  • 2 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號