我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用django.views.generic.TemplateView()。
def __init__(self, *args, **kwargs): super(MainMenu, self).__init__(*args, **kwargs) self.TOP_MENU_URLS = { "1": reverse('topmenu:listings', kwargs={"category": "for_sale"}), "2": reverse('topmenu:listings', kwargs={"category": "jobs"}), "3": reverse('topmenu:listings', kwargs={"category": "rides"}), "4": reverse('topmenu:listings', kwargs={"category": "announcements"}), "5": reverse('topmenu:voted_listings', kwargs={'category': 'commentary'}), "6": reverse('topmenu:voted_listings', kwargs={'category': 'emergency'}), # "0": reverse('topmenu:user_dashboard'), # special development session flush "000": reverse('topmenu:session_flush'), } # after being passed from below-written post method to generic view get(), # request and *args, **kwargs are passed are passed get_context_data(). # After context dict is generated, render_to_response in TemplateResponseMixin # (method of TemplateView) applies context and returns back to TwilioResponseMixin.dispatch()
def get_context_data(self, **kwargs): # get context data from TemplateView class context = super(LogsView, self).get_context_data(**kwargs) prev_date = timezone.now() - relativedelta(months=1) logs_delete = LogEntry.objects.exclude(log_datetime__gt=prev_date) if logs_delete: for log in logs_delete: log.delete() order_by = self.request.GET.get('order_by', '') if order_by in ('signal', 'status', 'log_datetime'): logs = LogEntry.objects.all().order_by(order_by) if self.request.GET.get('reverse', '') == '1': logs = logs.reverse() else: logs = LogEntry.objects.all().order_by('log_datetime').reverse() context = paginate(logs, 10, self.request, {}, var_name='logs') # check if we need to display some specific month return context # realisation checkboxes for group action
def test_view_inheritance(self): """Test view inheritance name""" assert isinstance(self.view(), TemplateView) assert isinstance(self.view(), RatelimitMixin)
def test_view_inheritance(self): """Test view inheritance name""" assert isinstance(self.view(), TemplateView)
def test_conditional_translate_bidi_template(rf): class View(ConditionalEnableTranslationsMixin, TemplateView): template_name_bidi = 'bidi.html' template_name = 'non-bidi.html' view = View.as_view() request = rf.get('/') request.LANGUAGE_CODE = 'ar' response = view(request) assert response.status_code == 200 assert response.template_name == ['bidi.html']
def test_conditional_translate_non_bidi_template(rf): class View(ConditionalEnableTranslationsMixin, TemplateView): template_name_bidi = 'bidi.html' template_name = 'non-bidi.html' view = View.as_view() request = rf.get('/') request.LANGUAGE_CODE = 'en-gb' response = view(request) assert response.status_code == 200 assert response.template_name == ['non-bidi.html']
def post(self, request, pk, *args, **kwargs): data = json.loads(self.request.body) sites = data.get('sites') users = data.get('users') group = Group.objects.get(name=data.get('group')) user = request.user task = multiuserassignsite.delay(user, pk, sites, users, group.id) if CeleryTaskProgress.objects.create(task_id=task.id, user=user, task_type=2): return HttpResponse('sucess') else: return HttpResponse('Failed') # if(Group="Reviewer or Site Supervisor") and request.user not in test # return reverse redirect login # if(Gropp="Project Manager")and not in request.user not in test # return reverse redirect login # class MultiUserAssignSiteView(ProjectRoleMixin, TemplateView): # def get(self, request, pk): # project_obj = Project.objects.get(pk=pk) # return render(request, 'fieldsight/multi_user_assign.html',{'type': "site", 'pk':pk}) # def post(self, request, *args, **kwargs): # data = json.loads(self.request.body) # sites = data.get('sites') # users = data.get('users') # group = Group.objects.get(name=data.get('group')) # response = "" # for site_id in sites: # site = Site.objects.get(pk=site_id) # for user in users: # role, created = UserRole.objects.get_or_create(user_id=user, site_id=site.id, # project__id=site.project.id, organization__id=site.project.organization_id, group=group, ended_at=None) # if created: # # description = "{0} was assigned as {1} in {2}".format( # # role.user.get_full_name(), role.lgroup.name, role.project) # noti_type = 8 # # if data.get('group') == "Reviewer": # # noti_type =7 # # noti = role.logs.create(source=role.user, type=noti_type, title=description, # # description=description, content_type=site, extra_object=self.request.user, # # site=role.site) # # result = {} # # result['description'] = description # # result['url'] = noti.get_absolute_url() # # ChannelGroup("notify-{}".format(role.organization.id)).send({"text": json.dumps(result)}) # # ChannelGroup("project-{}".format(role.project.id)).send({"text": json.dumps(result)}) # # ChannelGroup("site-{}".format(role.site.id)).send({"text": json.dumps(result)}) # # ChannelGroup("notify-0").send({"text": json.dumps(result)}) # # Device = get_device_model() # # if Device.objects.filter(name=role.user.email).exists(): # # message = {'notify_type':'Assign Site', 'site':{'name': site.name, 'id': site.id}} # # Device.objects.filter(name=role.user.email).send_message(message) # else: # response += "Already exists." # return HttpResponse(response)
def post(self, request, pk, *args, **kwargs): data = json.loads(self.request.body) projects = data.get('projects') users = data.get('users') group = Group.objects.get(name=data.get('group')) group_id = Group.objects.get(name="Project Manager").id user = request.user task = multiuserassignproject.delay(user, pk, projects, users, group_id) if CeleryTaskProgress.objects.create(task_id=task.id, user=user, task_type=1): return HttpResponse("Sucess") else: return HttpResponse("Failed") #May need it # class MultiUserAssignProjectView(OrganizationRoleMixin, TemplateView): # def get(self, request, pk): # org_obj = Organization.objects.get(pk=pk) # return render(request, 'fieldsight/multi_user_assign.html',{'type': "project", 'pk':pk}) # def post(self, request, *args, **kwargs): # data = json.loads(self.request.body) # projects = data.get('projects') # users = data.get('users') # group = Group.objects.get(name="Project Manager") # for project_id in projects: # project = Project.objects.get(pk=project_id) # for user in users: # role, created = UserRole.objects.get_or_create(user_id=user, project_id=project_id, # organization__id=project.organization.id, # project__id=project_id, # group=group, ended_at=None) # if created: # description = "{0} was assigned as Project Manager in {1}".format( # role.user.get_full_name(), role.project) # noti = role.logs.create(source=role.user, type=6, title=description, description=description, # content_object=role.project, extra_object=self.request.user) # result = {} # result['description'] = description # result['url'] = noti.get_absolute_url() # ChannelGroup("notify-{}".format(role.organization.id)).send({"text": json.dumps(result)}) # ChannelGroup("project-{}".format(role.project.id)).send({"text": json.dumps(result)}) # ChannelGroup("notify-0").send({"text": json.dumps(result)}) # return HttpResponse("Sucess")