0

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>
12
  • 1
    I think you dont get any error, because error is happening inside the HTML. I think instead of views.home, you need to execute the views.search for "" url path. And are you sure data is correct? It seems like the URL is wrong and you are trying to use f strings but got the wrong variable (user). And then ofcourse, you will also have to pass the form onto the template inside the context dictionary for search view Commented Jun 23, 2022 at 1:08
  • 1
    The API is correct. But the URL you use in the commented code is different from the URL you use in the search function. To begin with, remove the f'...' in the URL and put in the beginning of the URL, that is how f strings work. And no, you can't map multiple views in one url. What are you trying to imply with user = n(name=n) ? Commented Jun 23, 2022 at 10:20
  • 1
    Yes so, you have to think the flow of data and then make a view like that. Is name from the form, same as the userid? Commented Jun 24, 2022 at 17:11
  • 1
    Where do you intend on saving? Did you make a model? Commented Jun 24, 2022 at 18:42
  • 1
    You just said, "I would like to save, the users input". If you dont need to save it in a database, then no, you dont need a model Commented Jun 24, 2022 at 18:44

1 Answer 1

1

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})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.