0

I trying to retrieve the information from my API via Y/views.py from another app in the same project. I am receiving the following error ([WinError 10061]). Although, i am able to perform get/post/put via my json_main.js. Thanks to all who support me in this journey. Please read what is already working. And as additional question I would ask why it is not possible to retrieve the data directly via my model/sqlite db.

I already created the following: X/Views + X/Template.html. X/Views renders information from the backend to the X/template.html. Within this template there is a JSON script(working) that performs a POST/GET/PUT to the API(working) on image click.

The function ultimately results in a new record in API/Bestellingen. The information stored via this JSON function (incl. token authentication; csrf) should now be retrieved in the views.py of Y/view. I already created an example dict. that is rendered to Y/template.html

I tried several JSON request methods (coreapi, urllib2, urllib3, requests), but keep receiving the error as mentioned before. As already stated: JSON.js script does work. Doing the same via POSTMAN also works.

Since i am performing the same via .js and postman, I am quite sure that the variables (token, header and the request) should be ok.

I will show some short snippets of already working code. Herafter i will show the code that doesn't seem to work.

Information from X/views.py -> X/template.html

return render(request, 'smaakjes/smaakjes.html', {'Drank': super_dict})

X/Template.html (see onclick ="")

{% for key, value_list in Drank.items %}
<img onclick="PostImageDetails(this)" style="margin: 0 auto;" src="{{   value }}" id="{{ value }}">

JSON.js (works), sends information to http:127:0:0:0:8000/api/bestellingen. I can see the stored information via the API view.

xmlhttp.send(JSON.stringify({'url':imageSrc, 'username': imageId}))

Y/views.py -> renders information to Y/template.html(works)

def BestellingenDashboard(request):

Data = {'iets': '1'}
return render(request, 'homepage.html', {'bestellingen' : Data})

Once information has been stored in the API/bestellingen, I would like to retrieve the whole json dict. via python Y/Views.py and store it in the above 'Data'(that's ofc easy :)). Does somebody know what i am doing wrong? Why is postman working? Am i Missing a header or something? In my opinion, retrieving data from your own API should be very easy :D

from django.http import HttpResponse
from django.shortcuts import render
from api.models import Bestellingen
import json
import secrets
import requests

URL = "http://127.0.0.1:8000/api/bestellingen/"
data_json = json.dumps(data)
payload = {'json_payload': data_json}

r = requests.get(url=URL, headers={'Content-Type': 'application/json', 'token' : secrets.token_hex(40)}, json=payload)
a = r.json()
print(a)

def BestellingenDashboard(request):

    Data = {'iets': '1'}
    return render(request, 'homepage.html', {'bestellingen' : Data})

As you can read, most is working (api, endpoints, apiviews, rendering info from X-X.html,X.html->API. I now want to get the information in Y.views, so that i can use this information in Y/template.html

ko_00
  • 118
  • 7
  • you're not giving us the exact error (full error trace). But in general, calling an API using `requests` inside your own django application won't work on a development machine using `runserver` because `runserver` is single-threaded and can't handle a request within another request. – dirkgroten May 12 '19 at 11:35
  • If both X and Y are part of the same broader application, try extracting the main functionality of Y into a function that you call directly from X (so you don't have to go through serialisation and deserialisation, including HTTP request/response cycles twice. If Y is really to be a separate micro-service (and will run with its own server in production), then run them also separately locally (with their own port). – dirkgroten May 12 '19 at 11:35
  • See also [this question](https://stackoverflow.com/questions/4808329/can-i-call-a-view-from-within-another-view) – dirkgroten May 12 '19 at 11:37
  • @dirkgroten thanks for your reply Dirk. If needed, i can provide you with the full error log. Although your first comment does apply to the situation. In production both X and Y will be runned from the same server. X is retrieving data from the user and stores it in the api. Y should retrieve that data. The question to which you referred does not seem to relate to mine? I am not calling a view from a view. I am calling saved data via json1 -> api back to api->view2, or db->view2. – ko_00 May 12 '19 at 12:19
  • 1
    If they are running on the same server, then you should not be calling your api from view2 with `requests`. You should be calling a python function in app X that returns you the data you need. It basically is the same as the question I mention, calling a view from another view (by calling the api from view2, you're effectively using a detour via the internet to call your view). Same as [this](https://stackoverflow.com/questions/54539157/how-do-i-make-a-post-request-from-within-a-django-function-to-rest-api) except you have a GET request. – dirkgroten May 12 '19 at 13:16
  • Basically, you want to refactor your X view (api view): An internal API function (that you can just import) that returns you the data you need, probably in the form of a python dict; and an external view (your API view) that uses this function to return a JsonResponse (and HttpResponse) by serialising this python dict. Then in Y, you call the internal API function, not the external API view. – dirkgroten May 12 '19 at 13:22
  • So if i understand you correctly, I can build a function in X.api.views.py that GETs the data from the api. I call that function from Y.views.py and render that data via a dict to my Y.template.home.html? What kind of function do i use for this? Is it a x = model.objects.all(), or a request? PS, i am pushing data from X with a json to the api. At the same time, Y should reflect that data in another html. It is similair to a dashboard. Also X.views.py retrieves the initial data from a folder structure, not from the api. – ko_00 May 12 '19 at 13:33
  • 1
    No, i'm saying that whatever data your Y.view needs from your X.api.view should not be an HttpResponse, but just python data/object. So instead of actually calling your X.api.view via HTTP, you should call some function inside X that gives you the data. And since that new function probably does mostly the same as X.api.view, you can call it also inside X.api.view to avoid duplicating code. – dirkgroten May 12 '19 at 13:36
  • Hey Dirk, thanks for pointing me in the right direction. Side note: I moved my classes from urls -> view.py. In someway this made 'PUT' options also available (not sure why though; guess back .v.s front-end security?). Nevertheless, i made a simple iteration in api(x).views and got the information that i wanted. Will this function (since it is in views) run each time a json request is sent from X template to Y API? – ko_00 May 12 '19 at 14:14
  • If it’s at the top level of the file yes. Wrap it in a function and the code will only run if the function is called. – dirkgroten May 12 '19 at 14:16

0 Answers0