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
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('web.urls'))
]
副urls:
from django.urls import path
from web import views
urlpatterns = [
path('', views.index),
path('login/', views.login),
path('lingqu/', views.lingqu),
]
models:
from django.db import models
# Create your models here.
class Favour(models.Model):
price = models.DecimalField(max_digits=5,decimal_places=2)
t_price = models.DecimalField(max_digits=5,decimal_places=2)
is_recommend = models.IntegerField(default=0)
shop = models.CharField(max_length=20)
class User(models.Model):
account = models.CharField(max_length=30)
password = models.CharField(max_length=30)
class User_favour(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
favour = models.ForeignKey(Favour,on_delete=models.CASCADE)
views:
from django.shortcuts import render,redirect
from django.http import HttpResponse
from web import models
import json
import re
# Create your views here.
def index(request):
user_id = request.session.get('user_id')
favours = models.Favour.objects.all()
return render(request,'index.html',locals())
def login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
if all([username,password]):
u1 = models.User.objects.filter(account=username,password=password).first()
ret = re.match(r"1[35678]\d{9}", username)
if ret:
if u1:
request.session['user_id'] = u1.id
return redirect('/')
else:
return HttpResponse('账号密码不正确')
else:
return HttpResponse('手机号格式不对')
else:
return HttpResponse('不能为空')
return render(request,'login.html')
def lingqu(request):
if request.method == 'POST':
id = request.POST.get('id')
user_id = request.session.get('user_id')
print(id,user_id)
mes = {}
if user_id:
f = models.Favour.objects.filter(id = id).first()
f.is_recommend = 1
f.save()
models.User_favour.objects.create(user_id = user_id,favour_id = id)
print(f.id)
mes['code'] = 200
mes['message'] = f.id
return HttpResponse(json.dumps(mes))
else:
return redirect('/login/')
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>
{% for i in favours %}
<table >
<tr>
<td rowspan="2">{{ i.price }}</td>
<td>满{{ i.t_price }}可用</td>
{% if i.is_recommend == 0 %}
<td><span id="{{ i.id }}"><button οnclick="lingqu({{ i.id }})">立即领取</button></span></td>
{% else %}
<td><button οnclick="yijing()">已经领取过</button></td>
{% endif %}
</tr>
<tr>
<td>仅可购买{{ i.shop }}的商品</td>
</tr>
</table>
<hr>
{% endfor %}
</body>
<script type="text/javascript">
function lingqu(id) {
$.ajax({
url:"/lingqu/",
type: "post",
dataType:"json",
data:{'id':id},
success:function (res) {
if(res.code == 200){
var fid = res.message
$('#'+fid).html('<button οnclick="yijing()">已经领取过了</button>')
}
else {
console.log(res)
}
}
})
}
function yijing() {
alert('已经领取过了')
}
</script>
</html>
login:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
账号: <input type="text" name="username">
密码: <input type="text" name="password">
<button type="submit">提交</button>
</form>
</body>
</html>