How Django process the post request sent from React? I have extracted the data from the POST request, but how show the data in a web browser?
I'm absolutely new to the field of frontend and backend. I sent a request from React to Django using "POST" and I have extracted data from the request I can print it in the terminal, but how to show the result in web browser from Django (i.e.8000/result/) it seems to use "GET" method but it fails because my data is extracted from POST request. So basicacly, I input a text and submitted to localhost:8000/result, so I want to show the result on this url or redirect to another and send it back to React.
I don't know how to achieve this I used a pretty dumb method I save the data of request in tempory json and read the json in another function to render a browser through "GET". I tried to render or redirect to some url pages directly after process the "POST" request, but it apprently fails.
views.py
@api_view(["GET","POST"])
#class SubmitTextView(View):
def post(request):
if request.method =="POST":
#print(True)
text = request.body
#print(type(text))
result = json.loads(text)["text"]
json_data = json.dumps({"text":result})
#return HttpResponse(json_data, content_type='application/json')
#return JsonResponse({"text":result})
#context = {'result':result,'headline':'Result Searched from Wikipedia'}
#return render(request, 'my_template.html', context)
with open("sample.json", "w") as outfile:
outfile.write(json_data)
return JsonResponse({"status":"success"})
def upload(request):
with open('sample.json', 'r') as openfile:
# Reading from json file
json_object = json.load(openfile)
text = json_object["text"]
#print(text)
context = {'result':text,'headline':'Result Searched from Wikipedia'}
return render(request, 'my_template.html', context)
url.py
from django.contrib import admin
from django.urls import path,include
from app.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path('new/',post,name = 'new'),
path('response/', upload, name='response'),
]
html
<h1>{{ headline }}</h1>
<div
style="border: 1px solid black; overflow: auto; max-height: 200px;">
<p>{{result}}</p>
</div>
Please help me with this... Appreciate it!!