Issue
Okay so I looked through a few different slack posts on this ValueError, but it seemed most of them had to do with not returning render which it seems like I am doing that correct..?
I am sure it has to do with my if statements, just not sure what exactly or how to set the code up correctly so I can check the form request to the browser.
Edit: Following comments I took the is_valid check out for now just to see if I could get a new error, and it seems I am getting a name error. " name 'name' is not defined "
So it's not able to get the form users input into the api.
views.py:
from http.client import responses
from django.shortcuts import render
from .forms import SearchUser
from .search import search
def home(request):
if request.method == "POST":
form = SearchUser(request.POST)
form.cleaned_data["name"]
else:
return render(request, "main/home.html", {
'form': SearchUser(), # Reference to form
'userid': search(request),
# 'mmr':NA,
})
search.py:
import requests
def search(request):
data = requests.get(
f"https://americas.api.riotgames.com/riot/account/v1/accounts/by-riot-id/{name}/NA1?api_key=RGAPI-d1224a2c-9130-45ff-8c05-0656d56d105f")
return data.json()['puuid']
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 %}
forms.py:
from django import forms
class SearchUser(forms.Form):
name = forms.CharField(label="Name", max_length=200)
Solution
Generally, the view requires to return single HttpResponse, outside the conditions.
Try below view:
def home(request):
user_id = ''
form = ''
if request.method == "POST":
form = SearchUser(request.POST)
print('form is coming')
# form.cleaned_data["name"]
return HttpResponse('<h2> form submitted.</h2>') #just for testing purpose you can remove it.
else:
form = SearchUser()
user_id = search(request)
return render(request, "main/home.html", {
'form': form, # Reference to form
'userid': user_id,
# 'mmr':NA,
})
Note:
You should always return an HttpResponseRedirect after successfully dealing with POST data. This tip isn’t specific to Django; it’s good web development practice in general.
So,
from django.shortcuts import redirect def home(request): user_id = '' form = '' if request.method == "POST": form = SearchUser(request.POST) print('form is coming') # form.cleaned_data["name"] return redirect('some_path_name') #Redirection is a good practice. else: ... ...
Edit:
Try this code:
search.py
import requests
def search(request):
name = request.POST.get('name', 'novalue')
if name != 'novalue':
print('--------------------------------')
print(name)
print('--------------------------------')
data = requests.get(
f"https://americas.api.riotgames.com/riot/account/v1/accounts/by-riot-id/{name}/NA1?api_key=RGAPI-d801ef44-e952-43b0-9b08-92badc7da82c")
print(data.url)
try:
puuid = data.json()['puuid']
if puuid:
print('-------------------this is ppuid-------------')
print(puuid)
print('-------------------this is ppuid-------------')
print(name, 'has come here, means 200')
return puuid
except:
print(name, 'has come here, means not found or forbidden')
return 'puuid not found'
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>
{% if userid != "puuid not found" %}
<p><strong>ppuid found - {{userid}}</strong></p>
{% else %}
<p><strong>{{userid}}</strong></p>
{% endif %}
{% endblock %}
views.py
from django.shortcuts import redirect, render
from django.shortcuts import HttpResponse
from .forms import SearchUser
from .search import search
def home(request):
user_id = ''
form = ''
if request.method == "POST":
form = SearchUser(request.POST)
if form.is_valid():
print('form is coming')
name = form.cleaned_data["name"]
print(name)
user_id = search(request)
return render(request, 'main/home.html', {'form': form, 'userid': user_id})
else:
form = SearchUser()
user_id = search(request)
return render(request, "main/home.html", {
'form': form, # Reference to form
'userid': user_id,
# 'mmr':NA,
})
Answered By - Sunderam Dubey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.