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()
Community
  • 1
  • 1
loremIpsum1771
  • 2,497
  • 5
  • 40
  • 87
  • 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. – knbk Dec 19 '15 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. – dyeray Dec 19 '15 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? – Daniel Roseman Dec 19 '15 at 15:33
  • @dyeray I just updated the post with what I've tried so far – loremIpsum1771 Dec 19 '15 at 16:08
  • @DanielRoseman When using ajax, I was getting xmlhttp request error and was informed that this was because the api doesn't allow ajax which is from the client side. I've updated the post with the problem I'm having now, thanks – loremIpsum1771 Dec 19 '15 at 16:11
  • 1
    You asked a [similar question](http://stackoverflow.com/questions/34301988/django-how-to-asynchronously-load-json-file-from-url) a few days ago, too. Why not just put a bounty on that question instead of posting a duplicate? – xyres Dec 19 '15 at 16:25
  • I think you can first try the api call from the browser or from curl to see if the url is well formed -getting the url from a django shell for example- before seeing if there's something else. Also, you are using v1 of the api, maybe that has something to do with it? – dyeray Dec 19 '15 at 16:33
  • @dyeray I've tried the api call using [this link](http://api.bandsintown.com/events/search?artists[]=foals&location=New%20York,NY&radius=150&format=json&app_id=YOUR_APP_ID) where I added in the arguments myself and was even able to use ajax to parse the file (once I copied its context into a local file). When you say to try it from curl, do you mean that I should use the link that has the concatenation of the variables? – loremIpsum1771 Dec 19 '15 at 19:44
  • Exactly, you can put a breakpoint after creating the variable "url" and see what is in there and if it is correct. – dyeray Dec 19 '15 at 20:04
  • @dyeray So I actually just tried requesting the json file in the same way that I do in the code provided here but just in the python shell using the json on urllib2 modules. Whenever I copy a link directly from the url bar and use it for the request, it seems to be working. But for some reason, whenever, I use variables to be concatenated to the url, it returns an empty JSON object. Is there something wrong with the way I'm doing the concatenation based on the code I provide here? – loremIpsum1771 Dec 19 '15 at 23:07
  • As I said, the best way to know if the string is being built correctly is actually calling the code and printing the variable url after being created. The only problematic thing I see in the construction of the url is that spaces would have to be converted into %20 in the places that can have spaces, like the city name. To do the correct conversion you would have to pass every variable that you add to the url through the function urllib2.quote() – dyeray Dec 20 '15 at 01:24

1 Answers1

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"))
dyeray
  • 1,056
  • 6
  • 17
  • 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. – loremIpsum1771 Dec 20 '15 at 05:35
  • Actually, nvm it is printing the response! – loremIpsum1771 Dec 20 '15 at 05:45