Ben Mayo
Ben Mayo

Reputation: 1325

AttributeError: 'WSGIRequest' object has no attribute 'site' - wagtail, wagalytics

I'm trying to implement wagalytics within my new wagtail site (https://github.com/tomdyson/wagalytics).

I'm my error pops up at line 138 of views.py

site = Site.objects.get(hostname=request.site.hostname)

AttributeError: 'WSGIRequest' object has no attribute 'site'

When I change this to something like

try:
    site = Site.objects.get(hostname=request.site.hostname)
except:
    site = '127.0.0.1'

It works - or at least gets me onto the next problem.

Obviously I don't want to go chopping in a try/except block into the code in production - and I'd be better off for an understanding of what's happening here and how to resolve it.

Upvotes: 1

Views: 2515

Answers (2)

Dan Swain
Dan Swain

Reputation: 3108

See the Wagtail 2.9 release notes. Going forward you should use either {% wagtail_site %} or {{ page.get_site}} in templates and Site.find_for_request(request) in python code.

Upvotes: 6

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477533

This error arises because the request object has no .site attribute. You need to enable the CurrentSiteMiddleware [Django-doc] for that.

You do so by adding 'django.contrib.sites.middleware.CurrentSiteMiddleware' to tuple/list of the MIDDLEWARE settings. So the settings.py file should look like:

# settings.py

# …

MIDDLEWARE = [
    # …,
    'django.contrib.sites.middleware.CurrentSiteMiddleware',
    # …
]

# …

Upvotes: 4

Related Questions