Skip to content

KeyError "request" when load or send the JSON data via ajax. #67

@agusmakmun

Description

@agusmakmun

Screenshots

image

Desktop

  • OS: MacOSX, Windows, Linux
  • Browser Chrome
  • Version 92.0.4515

Solution:

I think we can just check whether request is filled.

def render(self, context):
    request = context.get('request', None)
    if request:
        qs = Announcement.objects.filter(
            publish_start__lte=timezone.now()
        ).filter(
            Q(publish_end__isnull=True) | Q(publish_end__gt=timezone.now())
        ).filter(
            site_wide=True
        )
        exclusions = request.session.get("excluded_announcements", [])
        exclusions = set(exclusions)
        if request.user.is_authenticated:
            for dismissal in request.user.announcement_dismissals.all():
                exclusions.add(dismissal.announcement.pk)
        else:
            qs = qs.exclude(members_only=True)
        context[self.as_var] = qs.exclude(pk__in=exclusions)
    return ""

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions