0

I've been working on a Django app in which I have a page where users can enter in strings in a form (for a query) which will be concatenated to the url using an api for getting concert data. I tried to request the urls using ajax, but apparently the server for the api doesn't allow the client to request the data, meaning that this will have to be done on the server side in python.

I've looked at some SO posts like this one and this other one but they both seem to have solutions that can be implemented when using python in a general way or without the use of a framework. How should I go about requesting this data on the server side within Django?

EDIT:

With the following code, I've been getting a 400 error for some reason. All that is done in the view is the request to the site based on the input from the form. I was just testing whether or not the variables could be concatenate to the URL but the code does seem to be set up so that the form data will be posted when the user clicks submit as I have another view with the same strucuture that sends an email with the form variables, so I'm not sure why this isn't working.

function from views.py:

def search(request):
    form = SearchForm(request.POST or None)
    if form.is_valid():
        form_artistSelect = form.cleaned_data.get("artist_select")
        form_city = form.cleaned_data.get("city")
        form_state = form.cleaned_data.get("state")
        mile_radius = form.cleaned_data.get("radius")
        print "testing"
        url = "http://api.bandsintown.com/events/search?artists[]=" + form_artistSelect + "&location=" +form_city+","+ form_state+"&radius="+ mile_radius + "&format=json&app_id=YOUR_APP_ID"
        data = json.load(urllib2.urlopen(url))
        print data
    context = {
        
        "form" : form
    }
    return render(request,"searchform.html" , context)

attributes of the form used on the page

class SearchForm(forms.Form):
    artist_select = forms.CharField()
    city = forms.CharField()
    state = forms.CharField()
    radius = forms.CharField()
11
  • 2
    Django is a server-side framework. If you need to use Python as a web client, you need to use the general tools provided by Python -- Django does not provide anything to act as a client. Commented Dec 19, 2015 at 15:28
  • Can you give more details on what is the way you want to implement it that makes you need to use an asynchronous request? I understand what you want to give to the user, but not the solution you are thinking on using. Commented Dec 19, 2015 at 15:31
  • 1
    What does "apparently the server for the api doesn't allow the client to request the data" mean? How is it preventing that? What errors do you see? Commented Dec 19, 2015 at 15:33
  • @dyeray I just updated the post with what I've tried so far Commented Dec 19, 2015 at 16:08
  • 1
    You asked a similar question a few days ago, too. Why not just put a bounty on that question instead of posting a duplicate? Commented Dec 19, 2015 at 16:25

1 Answer 1

1

Try this, I think the problem could be invalid characters in the url:

form_artistSelect = urllib2.quote(form.cleaned_data.get("artist_select"))
form_city = urllib2.quote(form.cleaned_data.get("city"))
form_state = urllib2.quote(form.cleaned_data.get("state"))
mile_radius = urllib2.quote(form.cleaned_data.get("radius"))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer! It seems that the POST is being made successfully. I'm not currently seeing the JSON response but I probably can get that to display by messing around with the variables after the request is made.
Actually, nvm it is printing the response!

Your Answer

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.