django 王中王1之登陆注册

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,re_path,include
from django.views.static import serve
from three.settings import UPLOAD_ROOT

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

 

副urls:

from django.contrib import admin
from django.urls import path,re_path,include
from web import views

urlpatterns = [
path('index/',views.Index.as_view()),
path('login/',views.Login.as_view()),
]

 

models:

from django.db import models

# Create your models here.

# 用户表 订单表 订单详情表 评论表

class User(models.Model):
id = models.AutoField(primary_key=True)
phone = models.CharField(max_length=11)
password = models.CharField(max_length=255)

class Meta:
db_table = 'user'


class Order(models.Model):
id = models.AutoField(primary_key=True)
num = models.CharField(max_length=30)
add_time = models.DateTimeField(auto_now_add=True)
price = models.DecimalField(max_digits=7,decimal_places=2)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)

class Meta:
db_table = 'order'

class OrderDetails(models.Model):
id = models.AutoField(primary_key=True)
add_time = models.DateTimeField(auto_now_add=True)
image_url = models.CharField(max_length=200)
content = models.CharField(max_length=200)
price = models.DecimalField(max_digits=7,decimal_places=2)
count = models.IntegerField()
order = models.ForeignKey(Order,on_delete=models.CASCADE)


class Meta:
db_table = 'orderdetails'


class Comments(models.Model):
id = models.AutoField(primary_key=True)
content = models.CharField(max_length=200)
image_url = models.CharField(max_length=200)
user = models.ForeignKey(User, on_delete=models.CASCADE)

class Meta:
db_table = 'comments'

 

 

 

 views:

from django.shortcuts import render,HttpResponse,redirect
import os,re
from three import settings
from django.views import View
from web.models import *

# Create your views here.

def uploadfile(img):
f = open(os.path.join(settings.UPLOAD_ROOT, '', img.name), 'wb')
for chunk in img.chunks():
f.write(chunk)
f.close()


class Index(View):
def get(self,request):
user_id = request.session.get('user_id')
user_name = request.session.get('user_name')
order = Order.objects.filter(user_id=user_id).all()
orderdetails = OrderDetails.objects.all()
comments = Comments.objects.filter(user_id=user_id).all()

return render(request,'qimo1/index.html',locals())
def post(self,request):
if request.method == 'POST':
user_id = request.session.get('user_id')
content = request.POST.get('content')
image_url = request.FILES.get('image_url')
uploadfile(image_url)
if user_id:
if content:
comments = Comments(content=content,user_id=user_id,
image_url='/upload/'+image_url.name)
comments.save()
return redirect('/index/')
else:
return redirect('/login/')

class Login(View):
def get(self,request):
return render(request, 'qimo1/login.html')
def post(self,request):
if request.method == 'POST':
name = request.POST.get('name')
password = request.POST.get('password')
user = User.objects.filter(phone=name).first()
if all([name,password]):
if re.match(r"^1[35678]\d{9}$", name) != None:
if user:
if user.password == password:
request.session['user_id'] = user.id
request.session['user_name'] = user.phone
return redirect('/index/')

 

 

 

 

html:

index:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>订单页面</title>
</head>
<body>
<table border="2">
{% for i in order %}
<tr>
<td colspan="6">时间日期:{{ i.add_time }} 订单编号{{ i.num }}</td>
</tr>
<tr>
{% for j in orderdetails %}
{% if i.id == j.order_id %}
<td><img src="{{ j.image_url }}" alt="" width="100px" height="100px"></td>
<td>{{ j.content }} 数量:{{ j.count }} 用户:{{ user_name }}</td>
{% endif %}
{% endfor %}
</tr>
<tr>
<td colspan="6">{{ i.price }}</td>
</tr>
{% endfor %}

</table>
<form action="" method="post" enctype="multipart/form-data">
请评论:<input type="text" name="content"><input type="file" name="image_url"><button type="submit">提交</button>
</form>
<ul>
{% for i in comments %}
<li>{{ i.content }}</li>
<li><img src="{{ i.image_url }}" alt="" width="70px" height="70px"></li>
{% endfor %}
</ul>
</body>
</html>

 

 

login:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="" method="post">
用户名:<input type="text" name="name"><br>
密 码:<input type="password" name="password"><br>
<button type="submit">登录</button>
</form>
</body>
</html>

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值