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:

<!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 onclick="lingqu({{ i.id }})">立即领取</button></span></td>

{% else %}

<td><button onclick="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 onclick="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>

原文地址:https://www.cnblogs.com/lhrd/p/10915064.html

时间: 2024-10-20 01:45:06

django 王中王3之优惠卷的相关文章

django 王中王8之踏青撒花

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 adminfrom django.urls import path,re_path,includefrom django.views.static import

django 王中王5之化妆品

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 adminfrom django.urls import pathfrom web import views urlpatterns = [ path('add_go

django 王中王7之牛大头

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 adminfrom django.urls import path,re_pathfrom django.views.static import servefrom l

django 王中王10之游记

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 adminfrom django.urls import path,include urlpatterns = [ path('admin/', admin.site.

django 王中王9之获得记录

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 adminfrom django.urls import path,include urlpatterns = [ path('admin/', admin.site.url

.15-浅析webpack源码之WebpackOptionsApply模块之插件王中王

总体过了一下后面的流程,发现Compiler模块确实不适合单独讲解,这里继续讲解后面的代码: compiler.options = new WebpackOptionsApply().process(options, compiler); 这行代码与之前设置options默认值非常相似,但是复杂程度根本不是一个次元的. 这一节只能简单的看一眼内部到底有多少东西,整理后源码如下: "use strict"; const OptionsApply = require("./Opt

姚贝娜追思会北京举行 冯小刚王中磊到场哀悼

姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀

Django admin 中抛出 &#39;WSGIRequest&#39; object has no attribute &#39;user&#39;的错误

这是Django版本的问题,1.9之前,中间件的key为MIDDLEWARE_CLASSES, 1.9之后,为MIDDLEWARE.所以在开发环境和其他环境的版本不一致时,要特别小心,会有坑. 将settings里的MIDDLEWARE_CLASSES默认配置顺序改成如下 MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.middleware.common.CommonMiddlewar

django数据库中的时间格式与页面渲染出来的时间格式不一致的处理

django数据库中的时间格式与页面渲染出来的时间格式不一致的处理. 在数据库里,时间是这样显示的: date: 2012-07-21 12:27:22 | date | datetime | NO | | NULL | | +-----------+-------------+------+-----+---------+----------------+ 在模板里用: <span>{{ post.date }}</span> 打印出来,时间格式是这样的: July 21, 20