3

Ok, so I've been going through the Rango, Django tutorial:

(http://www.tangowithdjango.com/book17/chapters/bing_search.html)

I am implementing mostly the same but altering it to make a request to the Guardian Open-Platform API. I think most of it is correct but everytime I try and search I get the CSRF token missing or incorrect error. I had a good search around and tried a lot of different solutions but nothing seems to work!

I have included the searchpage html, views and guardian_search model below.

search.html:

{% extends 'rango/base.html' %}
{% load staticfiles %}
{% block title %}Search{% endblock %}
{% block body_block %}
<div class="page-header">
 <h1>Search with Rango"</h1>
</div>
<div class="row">
 <div class="panel panel_primary">
 </br>
 <form class="form-inline" id="user_form" method="post" action="{ url 'search' %}">
 {% csrf_token %}
 <!-- Display the search form elements here -->
 <input class="form-control" type="text" size="50" name="query" value="" id="query" />
 <input class="btn btn-primary" type="submit" name="submit" value="Search" />
 <br />
 </form>
 <div class="panel">
 {% if result_list %}
 <div class="panel_heading"> 
 <h3 class="panel-title">Results</h3>
 <!-- Display search results in an ordered list -->
 <div class="panel-body">
 <div class="list-group">
 {% for result in result_list %}
 <div class="list-group-item">
 <h4 class="list-group-item-heading"><a href="{{ result.url }}">{{ result.title }}</a></h4>
 <p class="list-group-item-text">A summary here?</p>
 </div>
 {% endfor %}
 </div>
 </div>
 {% endif %}
 </div>
 </div>
 </div>
</div>
{% endblock %}

views.py:

from django.template import RequestContext
from django.shortcuts import render_to_response
from rango.bing_search import get_content
def search(request):
 result_list = []
 if request.method == 'POST':
 query = request.POST['query'].strip()
 if query:
 result_list = get_content(query)
 return render_to_response('rango/search.html', {'result_list':result_list})

guardian_search.py:

import requests
import pprint as pp
def get_content():
 api_url = 'http://content.guardianapis.com/search?'
 payload = {
 'q': raw_input(''),
 'api-key': 'api_key',
 'page-size': 10,
 'show-editors-picks': 'true',
 'show-elements': 'None', #'audio', 'image', 'video', 'all'
 'show-fields': 'None', #'headline' , 'body'
 'field': 'None',
 }
 response = requests.get(api_url, params=payload)
 data = response.json()
 urlList = []
 for item in data['response']['results']:
 urlList.append({
 'url': item['webUrl'],
 'title': item['webTitle']})
 pp.pprint(urlList)
 return urlList
if __name__ == '__main__':
 get_content()

If there's anything else that would be useful, let me know and I'll add it.

Thanks in advance!!

asked Mar 29, 2015 at 16:23
0

1 Answer 1

3

If you use render_to_response, you must provide a RequestContext to make the CSRF token work.

return render_to_response('rango/search.html',
 {'result_list':result_list},
 context_instance=RequestContext(request))

However the easier solution, as used by Django 1.7 version of your tutorial, is to use the render shortcut instead of render_to_response.

from django.shortcuts import render
render(request, 'rango/search.html', {'result_list':result_list})

As an aside, you have a missing % in action="{ url 'search' %}">.

answered Mar 29, 2015 at 16:48
Sign up to request clarification or add additional context in comments.

1 Comment

Great. That worked with the addition of passing the guardian_search function q as the raw_input search term. Thanks!!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.