question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preview mode with WSGI

See original GitHub issue

Issue Summary

When I’ve used Wagtail on localhost in development mode - all works fine. But when I moved it to production with uWSGI server - Preview mode is broken now.

Steps to Reproduce

I used new-style middleware classes:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'wagtail.wagtailcore.middleware.SiteMiddleware',
    'wagtail.wagtailredirects.middleware.RedirectMiddleware',
]

My server setup is: nginx -> uwsgi -> django wsgi application. All pages works fine (including admin panel). But when I press “Preview” button in editor, I get exception:

[2017-01-15 16:02:37.411] [ERROR     ] [django.request] [exception/handle_uncaught_exception] [p:21|t:140140641003264] || Internal Server Error: /wagtail/pages/15/edit/preview/
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 217, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 215, in _get_response
    response = response.render()
  File "/usr/local/lib/python3.6/site-packages/django/template/response.py", line 109, in render
    self.content = self.rendered_content
  File "/usr/local/lib/python3.6/site-packages/django/template/response.py", line 86, in rendered_content
    content = template.render(context, self._request)
  File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py", line 66, in render
    return self.template.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 208, in render
    return self._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 994, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 174, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 994, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 174, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 994, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 70, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 994, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 70, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 994, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 70, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 994, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 994, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 961, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.6/site-packages/django/template/library.py", line 203, in render
    output = self.func(*resolved_args, **resolved_kwargs)
  File "/usr/local/lib/python3.6/site-packages/wagtail/wagtailcore/templatetags/wagtailcore_tags.py", line 21, in pageurl
    return page.relative_url(context['request'].site)
AttributeError: 'WSGIRequest' object has no attribute 'site'

Technical details

  • Python version: Python 3.5.1
  • Django version: Django (1.10.4)
  • Wagtail version: wagtail (1.8)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

3reactions
gasmancommented, Jan 17, 2017

Hi @soar,

From the stack trace, it looks like something is failing during the middleware handling - it then tries to render the 500 error template, and fails on a {% pageurl %} tag because request.site is not defined.

Please can you try simplifying your 500 error template as much as possible (e.g. leave out any menu includes) so that the original error is revealed?

Also: please check that the site record under Settings -> Sites is accurate (including setting the port to 443, if you’re using HTTPS), as that seems to be a common cause of preview failures.

2reactions
an0o0nymcommented, Mar 17, 2018

Also: please check that the site record under Settings -> Sites is accurate (including setting the port to 443, if you’re using HTTPS), as that seems to be a common cause of preview failures.

Setting port to 443 in Settings -> Sites helped me! Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quick Configuration Guide — mod_wsgi 4.9.4 documentation
This document describes the steps for configuring mod_wsgi for a basic WSGI ... To make use of daemon mode for WSGI applications hosted...
Read more >
Flask Hello World App with Apache WSGI on Ubuntu 14 - 2020
First, we just run it with Python interpreter. Then, we'll serve the page via Apache with WSGI. Install and Enable mod_wsgi.
Read more >
Python (WSGI) Setup - ReadMe Documentation
You add the ReadMe middleware to your WSGI server. The middleware sends to ReadMe the request and response objects that your server generates...
Read more >
Python WSGI Applications - DEV Community ‍ ‍
WSGI · Application/Framework: Python web frameworks actually act as external scripts that conform to the WSGI protocol by implementing a callable ...
Read more >
How to Deploy Python WSGI Apps Using Gunicorn HTTP ...
Gunicorn is a stand-alone WSGI web application server which offers a lot of functionality. It natively supports various frameworks with its ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found