2021年11月24日 星期三

Python Django APP(8)

在之前學習裡有語法是新增專案的
django-admin.py startproject website
在Django還有很重要的就是APP,一個專案可由很多APP組成,APP可多個專案互相使用,所以寫出一個好的APP給專案通用是相當重要的
終端機下輸入
python manage.py startapp staff
產生出Staff的APP
接著在Staff下的views.py新增一個function
def staffhello(request,name):         
    return render(request, 'staff_hello.html', {"hello_str": "你好啊{name},這是來自Staff APP!!".format(name=name)})
staff下新增一個templates資料夾,然後再新增一個staff_hello.html
<!DOCTYPE html>
<html>
<head>
    <title>staff_hello</title>    
</head>
<body>
<h1>{{ hello_str }}</h1>
</body>
</html>
修改website底下的urls.py
from staff.views  import  staffhello
urlpatterns = [
    path("staff/<str:name>",staffhello),
]
重點來了上述做完後如果網站跑起來會有錯誤
必須註冊APP
在setting.py中修改
#原本
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
#修改後
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'staff',
]

2021年11月22日 星期一

Python Django Template 語法(7)

在template中的註解
{# template中的註解寫法單行 #}
{% comment %}
template中的註解寫法多行
{% endcomment %}
if esle
{% if xxx == 5 %}      
{% elif xxx > 4  %} 
{% else %}
{% endif %} 
for迴圈
{% for xxx in result%}   
    {% comment %}      
    以下內容可試看看      
    forloop.counter0
    forloop.counter
    forloop.revcounter
    forloop.revcounter0
    forloop.first
    forloop.last
   {% endcomment %}   
   {{forloop.counter}}
    
{% empty %}
	無資料!!
{% endfor %}

2021年11月18日 星期四

Python Django Template(6)

承上篇路徑跟view已經可以搭配了
但這樣畫面看起來就只有文字
所以要搭配html在瀏覽器上看起來才會比較正常
首先要回到settings.py找到如下
#系統預設
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
#修改成
import os
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\', '/')],#templete路徑
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
新增一個templates的資料夾
在views.py中寫入function來回傳內容
from django.shortcuts import render
def hello(request):
    return render(request, 'hello.html', {"hello_str": "你好啊!!"})
在templates的資料夾中新增一個hello.html
內容自訂主要使用h1標籤來看是否有套版
<!DOCTYPE html>
<html>
<head>
    <title>您好套版</title>    
</head>
<body>
<h1>{{ hello_str }}</h1>
</body>
</html>
urls.py修改
urlpatterns = [   
    path("hello/",hello),
]
接著瀏覽器輸入
http://127.0.0.1:8000/hello
參考來源
Templates · Django Girls 學習指南

Python Django urls .py and views.py(5)

這次練習urls.py
urls.py可指定目前所有連結但需配合著views.py(可自取名字)來顯示
在website資料夾下建立一個views.py
在view.py中寫入function來回傳內容
from django.http import HttpResponse
def home(request):
      return HttpResponse("yes this is your home success!")
接著修改urls.py內容
#原本
from django.contrib import admin
from django.urls import path
urlpatterns = [
     path("admin/", admin.site.urls),
]
#修改後
from django.contrib import admin
from django.urls import path
from website.views import home #要import進來
urlpatterns = [
    #path("admin/", admin.site.urls),
    path("homexx/", home), #(網頁路徑,function)
]
接著瀏覽器輸入
http://127.0.0.1:8000/homexx/
更進階一點views.py加入
def yourinfo(request,name, age):
    return HttpResponse("您的名字{name},您的年齡{age}".format(name = name ,age = age))
修改urls.py
from website.views import home,yourinfo #要import進來
urlpatterns = [
    path("homexx/", home), 
    path("yourinfo/<str:name>/<int:age>",yourinfo),
]
接著瀏覽器輸入
http://127.0.0.1:8000/yourinfo/陳00/35

Python Django settings.py(4)

承上篇建置Django網站後會產生幾個檔
本篇大約簡單說明settings.py裡幾個比較常會使改到的
更進階後面會依文章需求補上
#這個開發時可True,正式發布時請一定要改False
DEBUG = True
#預設語言為en-us
#後期版本已經沒有zh-TW了請使用zh-hant(漢字繁體) or zh-hans(漢字簡體)
LANGUAGE_CODE = 'zh-hant'
多國語言可參考Using Language Identifiers (RFC 3066)
#系統時區預設UTC
TIME_ZONE = 'Asia/Taipei'
時區可參考List of tz database time zones

2021年11月17日 星期三

Python Django 建置專案(3)

終端機下輸入
django-admin.py startproject website
產生如下圖目錄及檔案代表完成
本機啟動網站,終端機下輸入
cd website
python manage.py runserver
開啟瀏覽器查看是否成功
http://127.0.0.1:8000/

Python 安裝套件pip Django(2)

開啟VS Code 新增終端機

#安裝pip(以後套件都可以使用pip安裝即可)
python -m pip install -U pip

#查看目前已安裝套件
pip list

#安裝套件目前是3.2.9
pip install Django

#移除套件
pip uninstall Django

#安裝套件更新
python -m pip install -U Django

相關可安裝套件可參考pip的官網

Python 開發環境(1)

紀錄學習使用Python Django架站
作業系統Windows 10
開發軟體PythonVisual Studio Code基本上下一步安裝到底,只有Add Python To Path要打勾
Python初階語法學習推薦GrandmaCan -我阿嬤都會【python】4小時初學者Python教學有點底子看完應該就會
未來幾篇Django均以It’s Django - 用Python迅速打造Web應用這本書學習

2021年9月6日 星期一

FluentValidation 一個Model多個驗證方式

.Net Core驗證方式FluentValidation通常都會用以下方式
新增一個Validator Class,撰寫驗證要輸入的欄位
public XXXValidator()
{      
  RuleFor(x => x.xxxfiled).NotEmpty().WithMessage("請輸入欄位");          
}

DI注入Service
services.AddTransient<IValidator<AAAViewModel>, XXXValidator>();         

然後前端驗證
FluentValidation.Results.ValidationResult vResult = this._xxxValidator.Validate(model);
if (!vResult.IsValid)
{
  throw new Exception(Function_ModelStateError.FormatToString(vResult));
}        

但是如果有另一個Service也是要用AAAViewModel但是驗證的欄位不一樣呢?
先新增一個Class如下
//來源https://stackoverflow.com/questions/11607476/two-validators-for-one-model
public static class ValidationExtensions
{
     public static FluentValidation.Results.ValidationResult ValidateModel<TModel, TValidator>(this TModel model, TValidator validator, ModelStateDictionary modelState)
         where TModel : class
         where TValidator : AbstractValidator<TModel>
     {
         var result = validator.Validate(model);
         return result;
     }
}

新增另一個Validator Class,撰寫驗證要輸入的欄位
public YYYValidator()
{      
  RuleFor(x => x.yyyfiled).NotEmpty().WithMessage("請輸入欄位");          
}

DI注入另一個Service
services.AddTransient<IValidator<AAAViewModel>, YYYValidator>();         

然後前端驗證
FluentValidation.Results.ValidationResult vResult = model.ValidateModel(new YYYValidator(), ModelState);//修改這
if (!vResult.IsValid)
{
  throw new Exception(Function_ModelStateError.FormatToString(vResult));
}        

參考來源
Two Validators for one Models

2021年8月6日 星期五

Linq Filter Detail(Labdba string array Like)

常常忘記這語法所以來記錄一下
情境個人資料內有許多經歷或是學歷
某天人資部需求想撈出那些人是碩士及博士
通常資料表會這樣開
員工資料主檔
public class Staff
{
	public int StaffId { get; set; }
	public string Name { get; set; }
	public List EducationList { get; set; }//多種學歷	
}
學歷檔
public class Education
{
	public int EducationId { get; set; }
	public int EducationType { get; set; }//假設有另一個資料表紀錄ID的意思 碩士是6 博士是7
	public string SchoolName { get; set; }
}
抓取過濾碩士博士
int[] filter = new int[] { 6, 7 };
result = result.Where(x => x.EducationList.Any(y => filter.Contains(y.EducationType))).ToList();
string[] filter = searchTxt.Split(',' , ' ');               
var result = data.Where(x => temp.Any(s => x.text.Contains(s)  || x.content.Contains(s))).ToList();
備注用額外Detail符合的筆數
  int mIsCondition = data.Sum(x => x.detail.Where(y => y.iscondition).Count());

2021年3月30日 星期二

.Net Core EF Core Power Tools

之前有篇文章有寫到.Net Core 問題4 DB First後來發現另一個VS套件可以使用
1.先至官網安裝EF Core Power Tools
2.在專案上右鍵EF Core Power Tools
3.點選Add Database Connection
4.填寫連線內容(跟Framework的模型很像吧)
5.選擇Table 或其他DB功能
6.Scaffold-DbContext
Context name:輸入您要的(預設都自動帶出)
Namespace:輸入您要的(預設都自動帶出)
EntityType path:您要存放的路徑資料夾, 例如你想放在專案下的Models資料夾裡就輸入Models
...中間略可不設定用預設比較好
Pluralize or signularize generated object names:將產生的物件名稱複數化或單數化",設定為"勾選"時 後面多出"s"
Use table and column names directly from the database:使用資料表原始名稱(CX_Name沒打勾時會變成CxName)跟之前的-use-database-names一樣
Use DataAnnotation attributes to configure the model:
Custiomize code using Handlebars templates
Include connection string in generated code:連線字串放於Context中建議不勾
Install the EF Core provider pageage in the project

最後覺得還是下指令的方便
參考來源
EF Core Power Tools

2021年2月3日 星期三

Bootstrap 問題 找不到popper.js

Bootstrap4.5.3使用了bootstrap-select 或是bootstrap-multiselect都會遇到下面問題
Uncaught TypeError: Bootstrap's dropdowns require Popper.js
collapse.js:363 Uncaught TypeError: u is not a constructor
缺少了Popper.js檔可至cdn下載所需的但目前版本v2.x以上好像也不行
使用popper.js 1.16.1的話是可以的
參考來源
https://stackoverflow.com/questions/60837918/dropdown-js186-uncaught-typeerror-u-is-not-a-constructor-returning-when-i-clic
Bootstrap 4 error “Bootstrap dropdown require Popper.js”, with Aurelia CLI and Require.js

2021年1月14日 星期四

.Net Core HTTP ERROR 413.1 - Request Entity Too Large

目前在寫一個.net core上傳
ok這都很簡單網路上一堆code
Html
<form enctype="multipart/form-data" method="post" asp-controller="FileStream" asp-action="Upload">
    多選檔案:<input type="file" name="File" multiple  />    <input type="submit" value="上傳" />
</form>
Controller
private IWebHostEnvironment _hostingEnvironment;
private readonly IFunction_Error _functionerror;
public FileStreamController(IWebHostEnvironment environment, IFunction_Error functionerror)
{
  _hostingEnvironment = environment;            
}
public IActionResult Index()
{
  return View();
}

[HttpPost]       
public async Task<IActionResult> Upload(List<IFormFile> file)
{       
  string uploadfolder = Path.Combine(_hostingEnvironment.WebRootPath, "files");      
  foreach (IFormFile item in file)
  {
   if (item.Length > 0)
   {
     string filePath = Path.Combine(uploadfolder, item.FileName);
     using (Stream fileStream = new FileStream(filePath, FileMode.Create))
     {
       await item.CopyToAsync(fileStream);
     }
   }
  }
 return View();
 }
Ok很簡單吧
User:我今天上傳了80MB的檔案出現了以下問題
好吧只好找解法了大概如下
1.Program加入
 serverOptions.Limits.MaxRequestBodySize = long.MaxValue;
 ...
 
2.startup加入
services.Configure(options =>
{
  options.MultipartBodyLengthLimit = long.MaxValue;
  ....
});
 
3.Controller加入
 [DisableRequestSizeLimit]
 
以上三種都沒用最後註解掉
看到一篇加入一個web.config程式碼如下
居然通了80MB上傳沒問題
一直以為.net core是沒web.conifg的
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483647" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
User說:剛剛上傳了150MB的檔案都沒出現錯誤但檔案又沒傳上去
發現List<IFormFile> file是null但後端是有收到
感覺是大小沒問題只不過設定上有些不正確
接著就將startup那段註解的加了回去終於不null了
services.Configure(options =>
{
  options.BufferBodyLengthLimit = long.MaxValue;
  options.KeyLengthLimit = int.MaxValue;
  options.MultipartBodyLengthLimit = long.MaxValue;
  options.MultipartBoundaryLengthLimit = int.MaxValue;
  options.ValueCountLimit = int.MaxValue;
  options.ValueLengthLimit = int.MaxValue;
});
注意上面的MaxValue要修正阿不要真的那麼大還是要依專案來設定
參考來源
.net core issues: 413 Request Entity Too Large nginx
Kestrel web server implementation in ASP.NET Core [资源分享].netcore 部署时遇到413 Request Entity Too Large 和 413Payload Too Large 的问题 asp.net core 3.0 解除文件上传大小限制,500.30、413、api-post-form方法拿不到参数 解决方案 How to increase file upload size in ASP.NET Core https://www.aspsnippets.com/Articles/ASPNet-Core-IFormFile-always-returns-NULL.aspx .Net Core IFormFile 始终为空的问题