2
votes

i use django framework and i have this problem with the function list in views.py under: Failed to load resource: the server responded with a status of 403 (Forbidden) and i try to fix by using csrf_exempt and api_view please any body has a solution to help me and this my views :

views.py:

import json
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
#from django.views.decorators.http import require_POST
from rest_framework.decorators import api_view

from classes import JSONResponse
from models import Folder
from serializers import FolderSerializer


def index(request):
    return render(request, 'app/index.html', locals())


def api(request):
    folders = Folder.objects.all()
    serializer = FolderSerializer(folders, many=True)
    return JSONResponse(serializer.data)


@api_view(['POST'])
@csrf_exempt
#@require_POST
def list(request):
    data = json.loads(request.body)
    if data['path']=="/":
        rep = {"result": [
                            {
                                "name": "Dossier",
                                "rights": "drwxr-xr-x",
                                "size": "4096",
                                "date": "2016-03-03 15:31:40",
                                "type": "dir"
                            }, {
                                "name": "Fichier.txt",
                                "rights": "-rw-r--r--",
                                "size": "549923",
                                "date": "2016-03-03 15:31:40",
                                "type": "file"
                            }
                        ]}
    elif data['path']=="/Dossier":
        rep = {"result": [
                            {
                                "name": "Said.txt",
                                "rights": "-rw-r--r--",
                                "size": "549923",
                                "date": "2016-03-03 15:31:40",
                                "type": "file"
                            }
                        ]}
    return JSONResponse(rep)

urls.py:

from django.conf.urls import url

urlpatterns = [
    url(r'^$', 'app.views.index'),
    url(r'^api/', 'app.views.api'),
    url(r'^api/list', 'app.views.list'),
]
2
Can you post the full error from terminal or log file?trantu
And if you can post what's in your urls.pyserg

2 Answers

4
votes

thank you for all

i find the solution : the problem was the sessions of my admin were activated and to avoid the forbidden 403 problem you must logout to the admin page of django

0
votes

That feels like file permission issue. You are probably trying to access a file that the user doesn't have access to. Could you run with DEBUG=True and post the stack trace