django 王中王10之游记

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>

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

时间: 2024-10-20 01:45:09

django 王中王10之游记的相关文章

django 王中王7之牛大头

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 adminfrom django.urls import path,re_pathfrom django.views.static import servefrom l

django 王中王8之踏青撒花

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 adminfrom django.urls import path,re_path,includefrom django.views.static import

django 王中王5之化妆品

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 adminfrom django.urls import pathfrom web import views urlpatterns = [ path('add_go

django 王中王9之获得记录

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 adminfrom django.urls import path,include urlpatterns = [ path('admin/', admin.site.url

django 王中王3之优惠卷

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.

.15-浅析webpack源码之WebpackOptionsApply模块之插件王中王

总体过了一下后面的流程,发现Compiler模块确实不适合单独讲解,这里继续讲解后面的代码: compiler.options = new WebpackOptionsApply().process(options, compiler); 这行代码与之前设置options默认值非常相似,但是复杂程度根本不是一个次元的. 这一节只能简单的看一眼内部到底有多少东西,整理后源码如下: "use strict"; const OptionsApply = require("./Opt

王爽汇编10.12

;10.12 assume cs:code,ds:data data segment db 'word',0 db 'unix',0 db 'wind',0 db 'good',0 data ends code segment start : mov ax,data mov ds,ax mov si,0 mov di,16 mov cx,4 s: call cap inc si loop s MOV AX,4C00H INT 21H cap: push cx tip: mov cl,[si] m

姚贝娜追思会北京举行 冯小刚王中磊到场哀悼

姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀悼  姚贝娜追思会北京举行 冯小刚王中磊到场哀

django 模板中定义临时列表

<ul class="num_t clr"> {% for obj in ""|ljust:"10" %} <li>{{ forloop.counter }}</li> {% endfor %} </ul> 官网是这样使用: ljust Left-aligns the value in a field of a given width. Argument: field size For exampl