python project (server error and processing method)

Python is an object-oriented language, so the exceptions thrown by the program are also of the same class
. Access unknown object property ValueError The parameter type passed to the function is incorrect, such as passing a string shape to the int() function UnboundLocalError Unbound local error






 

(2) The following are the problems and solutions I have encountered recently:

①python local/local variable reference assignment problem

Refreshing the web server while working on a project prompts an error: UnboundLocalError: local variable 'user_sub' referenced before assignment   

So try to test out a solution

Server prompt (UnboundLocalError: reference to local variable 'user_sub' before assignment)

def cart(request):
    user_sub = 0 //Add this sentence when repairing, and assign a value to user_sub to solve it
    cartModel = CartModel.objects.all()
    total = 0
    for commodity in CartModel.objects.all():
        total += commodity.count
        total = int(total)
        user_sub = CartModel.sub_list(commodity, CartModel.objects.all())
        commodity.save()
    return render(request, 'cart/cart.html', {'cartModel':cartModel, 'total':total, 'user_sub':user_sub})

 At first, I added a global user_sub global variable attribute to user_sub, and then the server prompted user_sub is not defined.

Correctly corrected, I think the for loop belongs to a scope, there is an assignment to user_sub. But it cannot be seen from the outside, which is equivalent to referencing the local variable user_sub before assigning the variable user_sub.

 

②The server reports an error: AttributeError: 'int' object has no attribute 'save' (attribute error: 'int' object has no attribute' save)

According to the server prompt, I found the error line, this line function calculates the price of the product

  total += commodity.count
  total = int(total)
  count = commodity.count
  price = commodity.price
commodity.money = count * price
commodity.save()

 After the modification, it was found that the line was not aligned, and the last two lines were indented and aligned.

**Note**: Here is a reminder that python precedence is related to line alignment, and many small problems are also caused by line alignment

 

③ValueError: invalid literal for int() with base 10: 'Coca-Cola'

(meaning value error: invalid literal value for int() is 10: 'Coca-Cola')

The parameter type passed to the function is incorrect, such as passing a string shape to the int() function, which is the mistake I made

product_id = request.POST['id'] //this is the id sent by my POST request
//Now I check the output and find that the id passed is Chinese characters, so I can correct the POST sending data
 (Note here: id cannot be Chinese characters)

 

④The problem of referencing JS files

Server error: Uncaught ReferenceError: connect is not defined

This is a reference file problem, and the header can be imported, and the template page type

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
 //because $ is defined in jquery.js

 

⑤ HTML template page calculation error

item.count is the number of items, the preferential condition is buy three get one free

The price calculation formula in the model is

def act_price(self):
        self.tot_money = (self.count - int(self.count / 3)) * self.price
        self.save()

The gift amount can be calculated on the HTML page

At first I wrote

<span class='gift_num'>{% widthratio item.count 3 %}</span>

The server reports an error after running: (TemplateSyntaxError("widthratio takes at least three arguments")

It means TemplateSyntaxError ("widthratio requires at least three parameters"), so add parameter 1

The correct spelling is

<span class='gift_num'>{% widthratio item.count 3 1 %}</span>  //widthratio宽度比

 

(3) Supplement: Django template algorithm

<addition>: {{ value|add:10}}

value=5, return 15

<减法>:{{value|add:-10}}

value=5,则返回-5,减法就是加一个负数

<乘法>:{%  widthratio 5 1 100 %}

(注意:该算法会四舍五入,不适合小数点运算)

表示5/1 *100,返回500,widthratio需要三个参数,它会使用 参数1/参数2*参数3,所以要进行乘法的话,就将参数2设为1即可

<除法>:{%  widthratio 5 100 1 %}

表示:5/100*1,返回0.05,只需要将第三个参数设置为1即可 

(注意:参数个数不符合时,服务器会报错参数个数异常))

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326797958&siteId=291194637