Reputation: 42793
In settings.py I have:
BASE_DIR = Path(__file__).resolve().parent.parent
Then In some view:
from django.http import HttpResponse
from django.conf import settings
def test_view(request):
return HttpResponse( settings.BASE_DIR.replace("src", "") )
This gives error: replace() takes 2 positional arguments but 3 were given
It confuses me, how that error appears? also if do:
return HttpResponse( settings.BASE_DIR )
this returns full path, something like: /home/full/path/to/project/src
also this works
return HttpResponse( "/home/full/path/to/project/src".replace("src", "") )
Can you help me and tell what is wrong with this line:
return HttpResponse( settings.BASE_DIR.replace("src", "") )
?
Upvotes: 14
Views: 22015
Reputation: 15738
From Django 3.1 BASE_DIR is by default set to new pathlib module Path object as documented
from source
BASE_DIR = Path(__file__).resolve().parent.parent
Coincidentally Path has also .replace()
method but it does not have same use case as string replace
You might want to use instead parent
accessor:
settings.BASE_DIR.parent
Upvotes: 2
Reputation: 885
You're not calling the replace
method of the str
stype, but the one of the Path
class from pathlib
(because BASE_DIR
is a Path
instance).
It only takes two args (eg. my_path.replace(target))
, therefore the exception.
Docs here about what is does (basically renaming a file or directory).
Cast your Path
instance to a string.
Upvotes: 6