I have the local and staging environment which I set using DJANGO_SETTINGS_MODULE. I want this URL to only be accessible in the staging environment. How can I know that the current environment is staging and restrict the URL to it. Here is my url
path("testing_page/", views.testing_page_view, name="testing_page"),
The staging base url is https://myapp-staging.domain.com/
The view is
def testing_page_view(request):
if request.method == "GET":
return render(request, "testing_page.html")
else:
values = request.POST.dict()
return HttpResponseRedirect(login_link)
CodePudding user response:
Access restrictions should be handled in your view. For example, you can raise a 404 exception if someone tries to access the path in the staging environment.
from djnago.conf import settings
from django.http import Http404
def testing_page_view(request):
if settings.YOUR_ENVIRONMENT_VARIABLE == 'staging':
#Here you can handle the case when the path is accessed in staging
raise Http404('Only accessible in local environment')
if request.method == "GET":
return render(request, "testing_page.html")
else:
values = request.POST.dict()
return HttpResponseRedirect(login_link)
CodePudding user response:
First you need to import the Django conf :
from django.conf import settings
Then depending on your case something like that could work :
"https://myapp-staging.domain.com/" if settings.DEBUG else "https://myapp.domain.com/"
This suppose that you have a debug that is different in staging and local, otherwise just add a new variable called STAGING = TRUE