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.*)$',serve,{'document_root':UPLOAD_ROOT}),
path('web2/',include('web2.urls')),
]
副urls:
from django.contrib import admin
from django.urls import path,re_path,include
from web2 import views
urlpatterns = [
path('index/',views.Index.as_view()),
path('add_cakecate/',views.AddCakecate.as_view()),
path('add_cake/',views.AddCake.as_view()),
path('add_cakeorder/',views.Addcakeorder.as_view()),
]
models:
from django.db import models
# Create your models here.
class CakeCate(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=30)
class Meta:
db_table = 'cakecate'
class Cake(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
image_url = models.CharField(max_length=255)
price = models.DecimalField(max_digits=7,decimal_places=2)
count = models.CharField(max_length=255)
cakecate = models.ForeignKey(CakeCate,on_delete=models.CASCADE)
class Meta:
db_table = 'cake'
class CakeOrder(models.Model):
id = models.AutoField(primary_key=True)
count = models.IntegerField()
phone = models.CharField(max_length=11)
address = models.CharField(max_length=100)
username = models.CharField(max_length=30)
content = models.CharField(max_length=255)
name = models.CharField(max_length=30)
views:
from django.shortcuts import render,HttpResponse,redirect
from django.views import View
from web2.models import *
from web.views import uploadfile
import json
# Create your views here.
class AddCakecate(View):
def get(self,request):
return render(request,'qimo3/add_cakecate.html')
def post(self,request):
name = request.POST.get('name')
if name:
cakecate = CakeCate(name=name)
cakecate.save()
return redirect('/web2/add_cakecate/')
class AddCake(View):
def get(self,request):
cakecate = CakeCate.objects.all()
return render(request,'qimo3/add_cake.html',locals())
def post(self,request):
data = request.POST
name = data['name']
price = data['price']
count = data['count']
cakecate = data['cakecate']
image_url = request.FILES.get('image_url')
uploadfile(image_url)
cake = Cake(name=name,price=price,count=count,cakecate_id=cakecate,
image_url='/upload/'+image_url.name)
cake.save()
return redirect('/web2/add_cake/')
class Index(View):
def get(self,request):
cakecate = CakeCate.objects.all()
return render(request,'qimo3/index.html',locals())
def post(self,request):
mes = {}
id = request.POST.get('id')
if id:
cake = Cake.objects.filter(cakecate=id).all()
cakelist = []
for i in cake:
dict = {}
dict['name'] = i.name
dict['price'] = float(i.price)
dict['count'] = i.count
dict['id'] = i.id
dict['image_url'] = i.image_url
cakelist.append(dict)
mes['code'] = 200
mes['cakelist'] = cakelist
return HttpResponse(json.dumps(mes))
class Addcakeorder(View):
def get(self,request):
id = request.GET.get('id')
cakename = Cake.objects.filter(id=id).first()
return render(request,'qimo3/add_cakeorder.html',locals())
def post(self,request):
data = request.POST
name = data['name']
count = data['count']
phone = data['phone']
address = data['address']
username = data['username']
content = data['content']
cakeorder = CakeOrder(name=name,count=count,phone=phone,
address=address,username=username,content=content)
cakeorder.save()
return redirect('/web2/index/')
html:
add_cake:
蛋糕名称:
蛋糕价格:
蛋糕简介:
添加图片:
选择分类:
{% for i in cakecate %}
{% endfor %}
add_cakecate:
添加蛋糕分类:
add_cakeorder:
产品名称:
预定数量:
收获姓名:
收获地址:
联系电话:
内容:
index: