Django请求后数据输出为无

2024-09-29 23:30:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过我的post请求输出bally和ballx的值(不使用任何表单作为项目要求), 我已经设法在不使用表单的情况下完成了post请求,但是信息显示为none-bally:none-ballx:none,我觉得我对python非常陌生。我错在哪里

这是views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
from pong.models import SimpleBot

def home(request, template='index.html'):
    axis = HttpResponse(play(request))
    return render(request, template, {})


def bot(request):
    ballx = request.GET.get('ballx')
    bally = request.GET.get('bally')
    paddley = request.GET.get('paddley')
    court = {'ballx': ballx, 'bally': bally, 'paddley': paddley}
    data = {
      'up': SimpleBot.simple_bot(court),
    }
    return JsonResponse(data)

def play(request):
    ballx = request.POST.get('ballx')
    bally = request.POST.get('bally')
    court = {'ballx ': ballx, 'bally ': bally }
    print(court)
    return HttpResponse(court)

模板/index.html

{% load static from static %}
<!DOCTYPE html>
  <head>
    <title>Pong</title>
  </head>
  <h1>Pong<h1>
  <body>
   <canvas id='pong' width='600' height='400'></canvas>
   <script src={% static "pong.js" %}></script>
  </body>
</html>
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('pong/', include('pong.urls')),
    path('', include('pong.urls')),
]

编辑-现在可以在尝试显示ballx和bally的两个更改后显示硬编码值 models.py

from django.db import models

class SimpleBot(models.Model):

    @classmethod
    def simple_bot(request, court):
        return court["bally"] > court["paddley"]

    @classmethod
    def post_bot(request, court):
        return court['ballx']

新视图.py


def play(request):
    ballx = request.POST.get('ballx')
    bally = request.POST.get('bally')
    court = {'ballx ': ballx, 'bally ': bally }
    print(court)
    data = {
      'ballx': SimpleBot.post_bot(court),
      'bally': 'hello'

Tags: djangofromimportgetreturnmodelsrequestdef

热门问题