Django : CSRF verification failed even after adding {% csrf_token %}

后端 未结 7 1320
轻奢々
轻奢々 2021-01-03 11:20

views.py:

def index(request):
    return render_to_response(\'index.html\', {})

def photos(request, artist):
    if not artist:
        r         


        
7条回答
  •  暖寄归人
    2021-01-03 11:52

    Try using the @csrf_protect decorator:

    from django.views.decorators.csrf import csrf_protect
    from django.shortcuts import render_to_response
    
    @csrf_protect
    def photos(request,artist):
        if not artist:
            return render_to_response('photos.html', {'error' : 'no artist supplied'})
        photos = get_photos_for_artist(artist)
        if not photos:
            logging.error('Issue while getting photos for artist')
            return render_to_response('photos.html', {'error': 'no matching artist found'})
        return render_to_response('photos.html', {'photos': photos})  
    

提交回复
热议问题