I would like to password-protect only certain parts of my Django app with basic-auth. I'd like to protect all URLs except anything under /api.
I'm trying to use the django-basicauth package to do this.
I've configured it as follows. My app has three parts:
/api
/candc
/places
In candc/localsettings.py I've added:
BASICAUTH_USERS = {
"myuser": "mypass"
}
The candc/urls.py file looks like this:
urlpatterns = [
path('', include('places.urls')),
path('api/1.0/', include('api.urls')),
]
Then in my places/views.py file, I've added decorators to the URLs I want to protect, like this:
from basicauth.decorators import basic_auth_required
@basic_auth_required(
def index(request):
template = loader.get_template('index.html')
return HttpResponse(template.render({}, request))
However, my app is asking for basic-auth protect on URLs under /api as well. (In fact it's not even showing the dialog, just returning 403s for any requests.)
The api app is using django-rest-framework, which I suspect may somehow be related to this problem.
How can I configure this so URLs under /api are not password-protected, but everything else is?
As long as API urls are located in
candc/urls.pythey belong to thecandcapp. Therefore the settings for that app incandc/localsettings.pywill apply for all API endpoints as well. So either you put your API stuff into a separate app (with separateurls.py) or you remove theBASICAUTH_USERSsetting fromlocalsettings.pyand add@basic_auth_requiredto all views of thecandcapp.