django 內建日誌
阿新 • • 發佈:2019-01-29
https://yiyibooks.cn/xx/django_182/topics/logging.html
settings.py
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'de_bang',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'mysql',
}
}
de_bang/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^upload$', views.Upload.as_view()),
]
urls.py
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^', include('de_bang.urls')), ]
models.py
from django.db import models import uuid # Create your models here. class Organization(models.Model): """組織類""" TYPE_CHOICE = ( (1, "開發"), (2, "運維"), (3, "產品"), (4, "測試"), ) name = models.CharField(unique=True, db_index=True, max_length=50) type = models.CharField(choices=TYPE_CHOICE, max_length=4) class Person(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4) NAME = models.CharField(null=False, verbose_name="姓名",max_length=20) ID = models.BigIntegerField(unique=True, db_index=True, null=False, verbose_name="工號") Group = models.ManyToManyField(Organization, verbose_name="組織")
upload.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Ajax非同步上傳圖片</title>
<script>
$(document).ready(function () {
// TODO: 管理上傳使用者頭像表單的行為
$("#upload_file").submit(function (e) {
e.preventDefault()// 阻止表單的預設事件
$(this).ajaxSubmit({
url: "/upload",
type: "post",
headers: {
"X-CSRFToken": getCookie("csrf_token")
},
success: function (resp) {
if (resp.errno == "0") {
// 展示資料
$("#user-avatar").attr("src", resp.data.avatar_url)
} else if (resp.errno == "4101") {
location.href = "/login.html"
} else {
alert(resp.errmsg)
}
}
})
})
</script>
</head>
<body>
<form method="post" action="/upload">
<input type="file" id="upload_file" value="瀏覽檔案">
</form>
</body>
</html>
view.py
from django.http import HttpResponse
from django.shortcuts import render
import logging
# Create your views here.
from django.views.generic.base import View
class Upload(View):
"""上傳圖片邏輯"""
def get(self, request):
return render(request, 'upload.html')
def post(self, request):
f = request.FILES.get('pic', None)
if f:
print(f.name)
return HttpResponse("上傳成功!")