Django视图未返回我想要的内容
我试图从一个表格中显示一个数字的乘法表。这是我的护照Django视图未返回我想要的内容,django,django-templates,django-views,Django,Django Templates,Django Views,我试图从一个表格中显示一个数字的乘法表。这是我的护照 <form method="post" action="/home/result/"> <p>Enter your name</p> <p><input type="text" name="nombre"></p> <p>Enter you last name</p> <p><input type=
<form method="post" action="/home/result/">
<p>Enter your name</p>
<p><input type="text" name="nombre"></p>
<p>Enter you last name</p>
<p><input type="text" name="apellido"></p>
<p>Enter number for the table of that number</p>
<p><input type="number" name="table1"></p>
<p><input type="submit" name="Submit"></p>
</form>
这是我的另一个视图result.html
<p>Hello {{ nombre }} {{ apellido }}</p>
{% for number in numbers %}
{{ number }}<br />
{% endfor %}
这里的问题是,传递给视图的“6”实际上是一个字符串。因此,当您将其相乘时,您正在执行字符串相乘,它只是重复字符串
要解决此问题,必须通过执行
int(table)
将数字转换为整数。这里的问题是,传递给视图的“6”实际上是一个字符串。因此,当您将其相乘时,您正在执行字符串相乘,它只是重复字符串
要解决这个问题,您必须通过执行
int(table)
将数字转换为整数,以便在给出的另一个答案上展开,当您从post正文中获取时,table是一个字符串。必须使用int()
函数将其转换为整数。在代码的上下文中:
def get_name(request):
numbers = []
if request.method == 'POST':
nombre = request.POST.get('nombre')
apellido = request.POST.get('apellido')
table = request.POST.get('table1', 0) # this adds a default, in case table1 is empty
table = int(table) # converts the string into an integer
...
然后你就继续说你是如何从那里来的
作为参考,除非您有非常非常好的理由这样做,否则您永远不应该在生产应用程序中使用
@csrf_-emption
若要在给出的另一个答案上展开,当您从post正文中获取该表时,它是一个字符串。必须使用int()
函数将其转换为整数。在代码的上下文中:
def get_name(request):
numbers = []
if request.method == 'POST':
nombre = request.POST.get('nombre')
apellido = request.POST.get('apellido')
table = request.POST.get('table1', 0) # this adds a default, in case table1 is empty
table = int(table) # converts the string into an integer
...
然后你就继续说你是如何从那里来的
作为参考,除非您有非常非常好的理由这样做,否则您永远不应该在生产应用程序中使用@csrf_-emption
请注意,如果用户不提交数字,int(table)
可能会导致ValueError
。如果使用and,Django将负责验证输入并将值转换为整数。请注意,如果用户不提交数字,int(table)
可能会导致ValueError
。如果使用and,Django将负责验证输入并将值转换为整数。
def get_name(request):
numbers = []
if request.method == 'POST':
nombre = request.POST.get('nombre')
apellido = request.POST.get('apellido')
table = request.POST.get('table1', 0) # this adds a default, in case table1 is empty
table = int(table) # converts the string into an integer
...