How to return user input using an api in Django views? i keep receiving an error 'WSGIRequest' object has no attribute 'request'
this is my code for views.py. I would like the user input to replace the value for the 'q' key in the querstring dict.
def index(requests):
return render(requests, 'index.html')
def news(requests):
if requests.method == 'POST':
search = 'POST'
url = "https://google-finance4.p.rapidapi.com/search/"
querystring[0] = search
querystring = {"q":"*airbnb*","hl":"en","gl":"US"}
headers = {
"X-RapidAPI-Key": "my api key",
"X-RapidAPI-Host": "google-finance4.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
return render(requests, 'news.html')
else:
return HttpResponse('Error')
someapp/urls.py
urlpatterns = [
path('', views.index, name ='home'),
path('news/', views.news, name="news")
]
mysite/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('news/', include('search.urls'))
]
templates/index.html
<form>
class="form-inline"
method="POST"
action="{{ 'news/' }}"
name="news"
>
<div class="input-group mb-3">
{%csrf_token%}
<input
type="text"
class="form-control"
placeholder="Enter Crypto"
name="search"
style="width: 50%; display: block; margin: 0 auto"
/>
<button
class="btn btn-outline-primary btn-md"
type="submit"
id="button-addon2"
>
Search
</button>
templates/news.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Crypto Talk</title>
</head>
<body>
<h1>{{news.views}}</h1>
</body>
</html>
Am I formatting my syntax in the h1 element incorrectly, would this even tell Django to place the response I am looking for in this h1 tag?
I didn't quite understand the question, maybe you meant another. Try:
search = requests.POST["search"] # name of input