Search code examples
jqueryajaxdjangodjango-ajax-selects

Django 403 forbidden error ajax (with csrf token) GET


I'm using django-ajax-selects to select a city from my database. When typing in the field, I get a 403 error (GET method). Here's the catch, it worked yesterday, and I didn't touch anything relevant.

Console log

Forbidden (Permission denied): /lookups/ajax_lookup/city
[30/Jan/2016 15:54:01]"GET /lookups/ajax_lookup/city?term=Lyon HTTP/1.1" 403 22

My form

<form enctype="multipart/form-data" id="JobOfferForm" action="" method="POST">
    {% csrf_token %}

    <div class="row">
        <div class="input-field col s12">
            <p class="grey-text">Ville</p>
            {{ jobOfferForm.city }}
        </div>
    </div>

    <button class="btn waves-effect waves-light" name="jobOfferFormOK" type="submit">Sauvegarder</button>
</form>

Thanks in advance.


Solution

  • I have figured it out!
    I forgot to include a check_auth method in my CityLookup, so it worked only for staff users... I should have read the documentation better.

    def check_auth(self, request):
        if not request.user.is_authenticated() or not request.user.has_beta_access:
            raise PermissionDenied
    

    Now it works!