Issue
I am not sure if I am going about this correctly, but I am not getting any errors it just reloads the page without displaying anything.
The issue could be with me trying to format the api string and input the users input there?
I also tried returning the variable as an HttpResponse, still made no difference.
Sorry just getting back into python, and just starting with Django.
Correct code should go something like this:
1.User inputs their name into the form 2. Page then displays the usersid.
Code:
views.py:
from urllib import response
from django.shortcuts import render
from django.http import HttpResponse
import requests
from .forms import SearchUser
import json
# Create your views here.
def home(response):
# data = requests.get(
# 'https://americas.api.riotgames.com/riot/account/v1/accounts/by-riot-id/ReallyBlue/NA1?api_key=RGAPI-6c5d9a2c-3341-4b0c-a0a5-7eafe46e54cf')
# userid = data.json()['puuid']
return render(response, "main/home.html", {
'form': SearchUser(), # include reference to your form
'userid': search,
# 'mmr':NA,
})
def search(response):
if response.method == "POST":
form = SearchUser(response.POST)
if form.is_valid():
n = form.cleaned_data["name"]
user = n(name=n)
user.save()
data = requests.get(
f"https://americas.api.riotgames.com/riot/account/v1/accounts/by-riot-id/'{user}'/NA1?api_key=RGAPI-6c5d9a2c-3341-4b0c-a0a5-7eafe46e54cf")
userid = data.json()['puuid']
return HttpResponse(userid)
else:
form = SearchUser()
return render(response, "main/home.html", {"userid": userid})
forms.py:
from django import forms
class SearchUser(forms.Form):
name = forms.CharField(label="Name", max_length=200)
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path("", views.home, name=""),
path("", views.search, name=""),
]
home.html:
{% extends 'main/base.html'%}
{% block content %}
<h2>Valorant Ranked Checker</h2>
<form method="post" action="">
{% csrf_token %}
{{form}}
<button type="submit" name="search">
Get rank
</button>
</form>
<p><strong>{{userid}} - {{mmr}}</strong></p>
{% endblock %}
base.html:
<!DOCTYPE html>
<head>
<title>Blue's Valorant Ranked Checker</title>
</head>
<body>
<div id="content" name="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
Solution
So what you have to do is, use a single view for the url and then do the API querying inside that view. So something like:
urlpatterns = [
path("", views.search, name=""),
]
and then inside your view, you need to send the form
with the context dictionary to show it in the HTML.
def search(response):
if response.method == "POST":
form = SearchUser(response.POST)
if form.is_valid():
name = form.cleaned_data["name"]
data = requests.get(
f"https://americas.api.riotgames.com/riot/account/v1/accounts/by-riot-id/{user}/NA1?api_key=*******")
userid = data.json()['puuid']
return HttpResponse(userid) # Or preferably pass onto a different view
else:
return HttpResponse("Form is not properly filled")
else:
form = SearchUser()
return render(response, "main/home.html", {"form": form})
Answered By - Delrius Euphoria
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.