django 王中王4之超难变价

setting:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]


UPLOAD_ROOT = os.path.join(BASE_DIR,'upload')
 
 
 
主urls:
from django.contrib import admin
from django.urls import path,include,re_path
from django.views.static import serve
from goods.settings import UPLOAD_ROOT

urlpatterns = [
path('admin/', admin.site.urls),
path('',include('web.urls')),
re_path('^upload/(?P<path>.*)$',serve,{'document_root':UPLOAD_ROOT}),
]
 
 
 
 
 
 
副urls:
from django.contrib import admin
from django.urls import path
from web import views

urlpatterns = [
path('', views.index),
]
 
 
 
 
models:
from django.db import models

# Create your models here.

class Good(models.Model):
name = models.CharField(max_length=20)
y_price = models.DecimalField(max_digits=5,decimal_places=2)
price = models.DecimalField(max_digits=5,decimal_places=2)
img = models.CharField(max_length=200)
number = models.IntegerField(default=0)

class User(models.Model):
name = models.CharField(max_length=20)


class Buy(models.Model):
good = models.ForeignKey(Good,on_delete=models.SET_NULL,null=True,blank=True)
user = models.ForeignKey(User,on_delete=models.SET_NULL,null=True,blank=True)
 
 
 
 
views:
from django.shortcuts import render,redirect
from web import models
# Create your views here.

def index(request):
user = 1
goods = models.Good.objects.all()
if request.method =='POST':
goods = request.POST.getlist('goods')
for i in goods:
models.Buy.objects.create(user_id = 1 , good_id = i)
return redirect('/')
return render(request,'index.html',locals())
 
 
 
 
 
html:
index:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="/static/jquery-1.12.4.min.js"></script>
</head>
<body>
<h3>选择换购商品</h3>
<form action="" method="post">
{% for i in goods %}
<input type="text" value="{{ i.number }}" id="LLL{{ i.id }}" hidden>
<table>
<tr>
<td rowspan="2"><input type="checkbox" name="goods" id="{{ i.id }}" value="{{ i.id }}" onclick="bian({{ i.id }})"></td>
<td rowspan="2"><img src="{{ i.img }}" alt="" width="100px" height="100px"></td>
{% if i.number == 0 %}
<td align="center" colspan="2">{{ i.name }}(无货)</td>
{% else %}
<td align="center" colspan="2">{{ i.name }}(有货)</td>
{% endif %}
</tr>
<tr>

<td> <span id="L{{ i.id }}">{{ i.price }}</span>元</td>
<td style="text-decoration:line-through;">{{ i.y_price }}元</td>
</tr>
</table>
{% endfor %}
已选 <span class="num">0</span>件,共计 ¥<span id="hehehe">0</span>
<hr>
<button type="submit">确定</button>
</form>
</body>
<script type="text/javascript">
function bian(id) {
var count = num = $('#LLL'+id).val()
var isChecked = $('#'+id).is(":checked");
var a = $('#L' + id).text()
var p = $('#hehehe').text()
var num = $('.num').text()
if(parseFloat(count) == 0){
$("#"+id).attr("checked",false);
alert('无货状态,不能选择')
}
else {
if(isChecked) {
var price = parseFloat(p) + parseFloat(a)
num = parseFloat(num) + 1
$('.num').html(num)
$('#hehehe').html(price)
}
else {
var price1 = parseFloat(p) - parseFloat(a)
num = parseFloat(num) - 1
$('.num').html(num)
$('#hehehe').html(price1)
}
}


}
</script>
</html>
 
 

猜你喜欢

转载自www.cnblogs.com/lhrd/p/10915079.html