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>