cmbind55 cmbind55 - 1 year ago 56
Python Question

TypeError when concatenating django.test.LiveServerTestCase's live_server_url with another string

Whenever I try to construct a string based on

, I get python
messages. For example, I've tried the following string constructions (form 1 & 2 below), but I experience the same
. My desired string is the Live Server URL with
appended. NOTE: the actual test does succeed to create a server and I can manually access the server, and more specifically, I can manually access the exact URL that I'm trying to build programmatically (e.g.

s occur with these string constructions.

# FORM 1
lists_live_server_url = '%s%s' % (self.live_server_url, '/lists')

# FORM 2
lists_live_server_url = '{0}{1}'.format(self.live_server_url, '/lists')


There is no python error with this form (nothing appended to string), albeit my test fails (as I would expect since it isn't accessing


Here is the python error that I'm getting.

/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Applications/ test functional_tests.lists_tests.LiveNewVisitorTest.test_can_start_a_list_and_retrieve_it_later /Users/myusername/PycharmProjects/mysite_proj
Testing started at 11:55 AM ...
Creating test database for alias 'default'...
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/", line 137, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python3.4/site-packages/django/test/", line 1104, in __call__
return super(FSFilesHandler, self).__call__(environ, start_response)
File "/usr/local/lib/python3.4/site-packages/django/core/handlers/", line 189, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.4/site-packages/django/test/", line 1087, in get_response
return self.serve(request)
File "/usr/local/lib/python3.4/site-packages/django/test/", line 1099, in serve
return serve(request, final_rel_path, document_root=self.get_base_dir())
File "/usr/local/lib/python3.4/site-packages/django/views/", line 54, in serve
fullpath = os.path.join(document_root, newpath)
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/", line 82, in join
path += b
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'

Am I unknowingly attempting to modify the
, which is leading to these
s? How could I programmatically build a string of
live_server_url + "/lists"

Here is the test that I am attempting...

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.test import LiveServerTestCase

class LiveNewVisitorTest(LiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()

def tearDown(self):

def test_can_start_a_list_and_retrieve_it_later(self):
#lists_live_server_url = '%s%s' % (self.live_server_url, '/lists')
#lists_live_server_url = '{0}{1}'.format(self.live_server_url, '/lists')
lists_live_server_url = self.live_server_url

self.assertIn('To-Do', self.browser.title)
header_text = self.browser.find_element_by_tag_name('h1').text
self.assertIn('To-Do', header_text)

Answer Source

See this discussion on Reddit featuring the same error Traceback.

Basically, this is not a problem with anything within the Selenium tests but rather with your project's static file configuration.

From your question, I believe the key line within the Traceback is:

File "/usr/local/lib/python3.4/site-packages/django/views/", line 54, in serve
    fullpath = os.path.join(document_root, newpath) 

This line indicates that an unsuccessful os.path.join is being attempted within django.views.static.

Set STATIC_ROOT in your project's settings.pyfile and you should be good.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download