无法加载资源:服务器响应状态为403(禁止)django框架

时间:2016-05-04 15:58:38

标签: python django

我使用django框架,我在views.py中的函数列表中遇到此问题:无法加载资源:服务器响应状态为403(禁止),我尝试使用csrf_exempt和api_view修复任何问题身体有一个解决方案来帮助我和我的观点:

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 个答案:

答案 0 :(得分:2)

谢谢大家

我找到了解决方案:问题是我的管理员的会话被激活了,并且为了避免禁用403问题,你必须退出到django的管理页面

答案 1 :(得分:0)

感觉就像文件许可问题一样。您可能正在尝试访问用户无权访问的文件。您可以使用class Resettable(object): '''A class with "resettable" fields.''' _resettables= {} def __init__(self): for field, value in self._resettables.items(): setattr(self, field, value) def __getattr__(self, attr): try: return self._resettables[attr] except KeyError: pass def reset(self, field): if field not in self._resettables: raise AttributeError('%s is not a resettable field' % field) else: try: delattr(self, field) except AttributeError: pass class A(Resettable): _resettables= dict(m1 = None, m2 = {}) 运行并发布堆栈跟踪