django 王中王3之优惠卷

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:
Title

{% for i in favours %}
{% if i.is_recommend == 0 %}
{% else %}
{% endif %}
{{ i.price }} 满{{ i.t_price }}可用
仅可购买{{ i.shop }}的商品

{% endfor %}

 
 
 
 
 
 
 
login:
Title
账号:
密码:
 
 
 
 
 
 
 
 
 
 
 

转载于:https://www.cnblogs.com/lhrd/p/10915064.html

你可能感兴趣的:(python,json,javascript,ViewUI)