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.contrib import admin
from django.urls import path,re_path
from web import views
from django.views.static import serve
from nonius.settings import UPLOAD_ROOT
urlpatterns = [
path("",views.Index.as_view()),
path("addnonius/",views.AddNonius.as_view()),
path("adduser/",views.AddUser.as_view()),
path("submit_image/",views.submit_image),
path("show/",views.Show.as_view()),
path("addshow/",views.Addshow.as_view()),
re_path('^upload/(?P
]
models:
from django.db import models
# Create your models here.
"""
nonius 设置字段 标题caption 图片 内容content使用图文混排
user 设置字段 头像image name姓名 个性签名 signature
"""
class User(models.Model):
name = models.CharField(max_length=50)
file = models.CharField(max_length=255)
signature = models.CharField(max_length=50)
class Nonius(models.Model):
caption = models.CharField(max_length=50)
image = models.CharField(max_length=255)
content = models.TextField()
nid = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
views:
from django.shortcuts import render,redirect
from django.views import View
# Create your views here.
from django.http import HttpResponse
import json
from web.models import *
from nonius.settings import UPLOAD_ROOT
import os
def uploadfile(img):
f = open(os.path.join(UPLOAD_ROOT,'',img.name),'wb')
for chunk in img.chunks():
f.write(chunk)
f.close()
# 图文混排上传路径
def submit_image(request):
if request.method == "POST":
img = request.FILES.get("file")
uploadfile(img)
mes = {}
mes['path'] = "/upload/" + img.name
mes['error'] = False
return HttpResponse(json.dumps(mes))
# 首页
class Index(View):
def get(self,request):
return render(request,"index.html")
#添加用户
class AddUser(View):
def get(self,request):
return render(request,"adduser.html")
def post(self,request):
name = request.POST.get("name")
file = request.FILES.get("file")
uploadfile(file)
signature = request.POST.get("signature")
user = User(name=name,file = '/upload/' +file.name,signature=signature)
user.save()
request.session["user_id"] = user.id
return redirect("/adduser/")
# 添加游标
class AddNonius(View):
def get(self, request):
return render(request, "addnonius.html")
def post(self,request):
caption = request.POST.get("caption")
image = request.FILES.get("image")
uploadfile(image)
countent = request.POST.get("countent")
user_id = request.session.get("user_id")
nonius = Nonius(caption=caption,image='/upload/'+image.name,content=countent,nid_id=user_id)
nonius.save()
return redirect("/addnonius/")
# 展示用户和游标
class Show(View):
def get(self,request):
user = User.objects.all()
nonius = Nonius.objects.all()
return render(request,"show.html",locals())
#展示游标详细
class Addshow(View):
def get(self,request):
nonius = Nonius.objects.all()
return render(request, "addshow.html", locals())
html:
addnonius:
addshow:
{% for j in nonius %}
{{ j.caption }}
{{ j.content|safe }}
{% endfor %}
返回首页
adduser:
index:
发表游记
show:
进入详情
{% for j in nonius %}
{{ j.caption }}
{{ j.content|safe }}
{% endfor %}
{% for i in user %}
{{ i.name }}
{{ i.signature }}
{% endfor %}