django csrf_token in search result url

喜你入骨 提交于 2021-01-02 05:50:53

问题


Have csrf in search result url. Don't know why is there and how to remove it. Search works nice. Here is URL

/search/?csrfmiddlewaretoken=675d1340034e094866d15a921407e3fc&q=testing

here is view:

def search(request):
    query = request.GET.get('q', '')
    rezult = []
    if query:
    qset1 = (
        Q(title__icontains=query) 
    )
    result = Product.objects.filter(qset1).distinct()
    if result.count() == 1:
        return HttpResponseRedirect(result[0].get_absolute_url())
    return render_to_response('search/search.html',{'query': query, 'result': result, },context_instance=RequestContext(request))

Thanks


回答1:


Remove {% csrf_token %} from your form in the template, you don't need it since you're making a GET request.




回答2:


you added {% csrf_token %} in your form. if you dont need csrf remove this from your form and add csrf_exempt.

look at this sample of django:

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_view(request):
     return HttpResponse('Hello world')



回答3:


I would assume that you've added the {% csrf_token %} within one of the search form's input element. That would cause the token to be submitted along with the form.

Check your search form template.



来源:https://stackoverflow.com/questions/15050571/django-csrf-token-in-search-result-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!