码迷,mamicode.com
首页 > 其他好文 > 详细

django 王中王10之游记

时间:2019-05-23 21:08:47      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:display   _id   ima   redirect   path   adf   ase   play   cts   

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<path>.*)$‘,serve,{"document_root":UPLOAD_ROOT})


]

 

 

 

 

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:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="/static/admin/js/jquery-1.12.4.min.js"></script>
<script src="/static/admin/tinymce/js/tinymce/tinymce.min.js"></script>
<script src="/static/admin/js/tinymce_setup.js"></script>
<title>Title</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
标题 <input type="text" name="caption"> <br>
图片 <input type="file" name="image"> <br>
内容 <input name="countent" id="rich_content"> <br>
<button type="submit">添加</button><br>
<a href="/show/">进入展示</a>
</form>
</body>
</html>

 

 

addshow:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for j in nonius %}
<img src="{{ j.image }}" width="100px" height="100px">
<hr>
{{ j.caption }}
<hr>
{{ j.content|safe }}
{% endfor %}
<hr>
<a href="/">返回首页</a>
</body>
</html>

 

 

 

 

adduser:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
姓名 <input type="text" name="name"> <br>
图片 <input type="file" name="file"> <br>
签名 <input type="text" name="signature"> <br>
<button type="submit">添加</button>
</form>
</body>
</html>

 

 

 

index:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/addnonius/">发表游记</a>
</body>
</html>

 

 

 

show:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/addshow/">进入详情</a>
<div class="app">
<div style="display:inline-block;background:pink;">
{% for j in nonius %}
<img src="{{ j.image }}" width="100px" height="100px">
</div>
<div style="display:inline-block;background:pink;">
{{ j.caption }}
{{ j.content|safe }}
</div>
{% endfor %}

<div style="display:inline-block;background:#0f0;margin-left:10px;">
{% for i in user %}
{{ i.name }} <br>
{{ i.signature }} <br>
<img src="{{ i.file }}" width="100px" height="150px">
{% endfor %}
</div>
</div>
</body>
</html>

 

django 王中王10之游记

标签:display   _id   ima   redirect   path   adf   ase   play   cts   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!