0

I have a problem with my urls in my urls.py i have an url that must only be accessed with ajax, but recently i found out that i can call the ajax function if i type the url in the addressbar

path('mydata-vacation/', views.mydata_vacations, name='mydata-vacations')

if i go to my-website/mydata-vacation/

will execute the ajax command and it will render the page How to prevent it from doing that?

my view is

def mydata_vacations(request):
    user_data = get_user_data(request)
    user_id = user_data['id']
    user_demands = Demands.objects.filter(user_id=user_id, type_of_demand='vacation').order_by('-id')
    context = user_data
    context['demands'] = user_demands
    return render(request, 'manager/vacation_table.html', context)

and ajax is

$.ajax({
            type: "POST",
            url: '/mydata-vacations/',
            async: true,
            data: {
                csrfmiddlewaretoken: $('[name="csrfmiddlewaretoken"]').val(),

            },
            success: function (response) {

                $(".table-row").html(response);
                $('.accept-btn').css('display','none');
                $('.vacations-btn').css('display', 'flex')

            }
        });

the code works well but my question is how to deal with the url not to be accessible trough the browser addressbar

ThunderHorn
  • 1,975
  • 1
  • 20
  • 42
  • 2
    Use the `request.is_ajax()` method in the view, and return a suitable response for non ajax responses, e.g. `from django.http import HttpResponseForbidden; return HttpResponseForbidden()`. – Alasdair Jun 28 '18 at 15:09
  • 1
    Thank you very much @Alasadair ! It worked :) – ThunderHorn Jun 28 '18 at 15:11

0 Answers0