python 视图main.views.home没有返回HttpResponse对象,而是返回None

kd3sttzy  于 2022-11-28  发布在  Python
关注(0)|答案(1)|浏览(166)

好吧,我看了看这个ValueError上的一些不同的松弛帖子,但似乎大多数都与不返回渲染有关,看起来我这样做是正确的...?
我确信这与我的if语句有关,只是不确定到底是什么或如何正确地设置代码,以便我可以检查对浏览器的表单请求。
编辑:在评论之后,我暂时 checkout is_valid,只是为了看看是否会出现新的错误,似乎我遇到了一个名称错误。“name 'name' is not defined“
因此它无法将表单用户输入到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)
az31mfrm

az31mfrm1#

一般情况下,视图要求返回单个HttpResponse,在条件之外。
请尝试以下视图:

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:在成功处理POST数据后,你应该总是返回一个HttpResponseRedirect。这通常是一种很好Web开发实践。
所以,

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:
         ...
         ...

编辑:

请尝试以下代码:
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,
    })

相关问题