您好,登錄后才能下訂單哦!
本篇內容介紹了“D語言怎么創建一個blog項目”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
1.首先拉取skeleton項目為基礎,文件命名為myprogect
git clone https://github.com/huntlabs/hunt-skeleton.git myproject cd myproject dub run -v
2.修改項目中config下的application.conf文件中關于數據庫的相關配置
hunt.database.default.driver=mysql hunt.database.default.host=127.0.0.1 hunt.database.default.port=3306 //接口 hunt.database.default.database=myblog //數據庫名稱 hunt.database.default.username=root //數據庫用戶名 hunt.database.default.password= //數據庫密碼 hunt.database.default.charset=utf8 hunt.database.default.prefix= hunt.database.default.enabled=true
3.首先實現Blog項目后臺管理;確定項目后臺功能并在config下創建admin.routes文件定義路由
GET /user/list user.users //用戶列表頁面 * /user/add user.add //用戶添加操作 GET /user/delete user.remove //用戶刪除操作 GET /user/update user.update //用戶修改頁面 POST /user/doup user.doupdate //用戶修改操作 GET /blog/list blog.blogs //文章列表頁面 * /blog/add blog.add //文章添加頁面 POST /blog/doadd/ blog/doadd //文章添加操作
4.在source--app下建立Blog模塊;在Blog中新建一個model模型,在model下創建Blog.d以及User.d文件,用來存放對應數據表中的數據;
//Blog.d module app.blog.model.Blog; import hunt.entity; import app.model.User; @Table("blog") class Blog : Model { mixin MakeModel; @AutoIncrement @PrimaryKey int id; //主鍵,自動增長 long created; //時間 string title; //文章標題 string description; //描述 string picture; //圖片 string content; //內容 @OneToOne() @JoinColumn("user_id") User user;//通過user_id關聯User表 } //User.d module app.blog.model.User; import hunt.entity; @Table("user") class User : Model { mixin MakeModel; @AutoIncrement @PrimaryKey int id; string username; //用戶名 string password; //密碼 string email; //郵箱 }
5.以model為基礎;在source--app--blog下新建repository文件,創建相應的BlogRepository.d、UserRepository.d文件;對數據進行操作
//BlogRepository.d module app.blog.repository.BlogRepository; import app.blog.model.Blog; import hunt.entity; class BlogRepository : EntityRepository!(Blog , int){ } //UserRepository.d module app.blog.repository.UserRepository; import app.blog.model.User; import hunt.entity; class UserRepository : EntityRepository!(User,int){ }
6.在source--app--blog下新建admin文件,在admin中創建BlogController.d、UserController.d來實現后臺管理;包括文章的管理以及對用戶的瀏覽、添加、刪除、修改
//BlogController.d module app.blog.controller.admin.BlogController; import hunt.framework; import app.blog.form.BlogForm; import app.blog.model.Blog; import app.blog.repository.BlogRepository; import app.blog.repository.UserRepository; class BlogController : Controller { mixin MakeController; //文章的添加 @Action string add(BlogForm form) { if(request.method == HttpMethod.POST) { auto result = form.valid(); if(result.isValid) { auto blog = new Blog ; blog.title = form.tit; blog.description = form.dept; blog.picture = form.pic; blog.content = form.cont; blog.created = time(); auto repo = new BlogRepository; repo.save(blog); return "successed!"; } view.assign("messages", result.messages()); } view.assign("blog", (form is null) ? new BlogForm : form); view.assign("users", (new UserRepository).findAll());//查出User的全部信息 return view.render("blog_add"); } //文章列表瀏覽 @Action string blogs() { auto repo = new BlogRepository; auto blogs = repo.findAll();//查找全部文章信息 view.assign("blogs",blogs); return view.render("blog_list");//跳轉到列表頁面 } }
//UserController.d module app.blog.controller.admin.UserController; import hunt.framework; import app.blog.form.UserForm; import app.blog.model.User; import app.blog.repository.UserRepository; class UserController : Controller { mixin MakeController; //用戶添加 @Action string add(UserForm form) { if (request.method == HttpMethod.POST) { auto result = form.valid(); if(result.isValid) { auto user = new User; user.username = form.um; user.password = form.pwd; user.email = form.mail; auto repo = new UserRepository; repo.save(user); return "successed"; } view.assign("messages",result.messages()); } view.assign("user",(form is null) ? new UserForm : form); return view.render("user_add"); } //用戶列表瀏覽 @Action string users() { auto repo = new UserRepository; auto users = repo.findAll(); view.assign("users",users); return view.render("user_list"); } //用戶刪除操作 @Action string remove(int id) { auto repo = new UserRepository; repo.removeById(id);//根據id刪除用戶信息 return "deleted"; } //用戶修改列表 @Action string update(int id) { auto repo = new UserRepository; auto user = repo.find(id);//通過id查找要修改的信息 view.assign("user",user); return view.render("user_up");//跳轉到修改頁面 } //用戶修改操作 @Action string doupdate(UserForm form) { auto repo = new UserRepository; auto user = repo.find(form.id.to!int); user.username = form.um; user.password = form.pwd; user.email = form.mail;//進行修改操作 repo.save(user);//保存 return "successed"; } }
7.在view下新建admin,在admin中新建header.html、footer.html、sidebar.html顯示列表以及目錄的CSS樣式;blog_list.html、blog_add.html、user_add.html、user_list.html、user_up.html來實現功能列表;顯示部分代碼
//blog_list.html <div class="card-body"> <table id="example2" class="table table-bordered table-hover"> <thead> <tr> <th>title</th> <th>name</th> <th>description</th> <th>picture</th> <th>content</th> </tr> </thead> {% for blog in blogs %} <tbody> <tr> <td>{{ blog.title }}</td> <td>{{ blog.user.username}}</td> <td>{{ blog.description }}</td> <td>{{ blog.picture }}</td> <td>{{ blog.content }} <a href="/admincp/blog/view?id={{ blog.id }}">Read Full</a></td> </tr> </tbody> {% endfor %} <tfoot> <tr> <th>title</th> <th>name</th> <th>description</th> <th>picture</th> <th>content</th> </tr> </tfoot> </table> </div>
//blog_add.html <form role="form" method="POST" action="/admincp/blog/add"> <div class="card-body"> {% if messages %} <div class="form-group"> <ul> {% for key, message in messages %} <li>{{ key }} : {{ message }}</li> {% endfor %} </ul> </div> {% endif %} <div class="form-group"> <label for="exampleInputPassword1">title</label> <input type="text" name="tit" value="{{blog.tit}}" class="form-control" id="exampleInputPassword1" placeholder="title"> </div> <div class="form-group"> <label for="exampleInputEmail1">name</label> <select name="nm"> <option value="0">-- 請選擇 --</option> {% for user in users %} <option value="{{ user.id }}" {% if blog.nm %}{% if user.id == int(blog.nm) %} selected {% endif %}{% endif %}>{{ user.username }}</option> {% endfor %} </select> </div> <div class="form-group"> <label for="exampleInputEmail1">description</label> <input type="text" name="dept" value="{{blog.dept}}" class="form-control" id="exampleInputEmail1" placeholder="description"> </div> <div class="form-group"> <label for="exampleInputEmail1">picture</label> <input type="text" name="pic" class="form-control" id="exampleInputEmail1" placeholder="picture"> </div> <div class="form-group"> <label for="exampleInputEmail1">content</label> <input type="text" name="cont" value="{{blog.cont}}" class="form-control" id="exampleInputEmail1" placeholder="content"> </div> </div> <!-- /.card-body --> <div class="card-footer"> <button type="submit" class="btn btn-primary"></a>添加文章</button> </div> </form>
//user_add.html </div> <form role="form" method="POST" action="/admincp/user/add"> <div class="card-body"> {% if messages %} <div class="form-group"> <ul> {% for key, message in messages %} <li>{{ key }} : {{ message }} </li> {% endfor %} </ul> </div> {% endif %} <div class="form-group"> <label for="exampleInputEmail1">Username</label> <input type="text" name="um" value="{{user.um}}" class="form-control" id="exampleInputEmail1" placeholder="username"> </div> <div class="form-group"> <label for="exampleInputPassword1">Password</label> <input type="password" name="pwd" value="{{user.pwd}}" class="form-control" id="exampleInputPassword1" placeholder="password"> </div> <div class="form-group"> <label for="exampleInputEmail1">Email address</label> <input type="text" name="mail" value="{{user.mail}}" class="form-control" id="exampleInputEmail1" placeholder="Enter email"> </div> </div> <!-- /.card-body --> <div class="card-footer"> <button type="submit" class="btn btn-primary">添加用戶</button> </div> </form> </div>
//user_list.html <div class="card-body"> <table id="example2" class="table table-bordered table-hover"> <thead> <tr> <th>username</th> <th>email</th> <th>Delete</th> <th>Update</th> </tr> </thead> {% for user in users %} <tbody> <tr> <td>{{ user.username}}</td> <td>{{ user.email}}</td> <td><a href="/admincp/user/delete?id={{ user.id }}">Delete</a></td> <td><a href="/admincp/user/update?id={{ user.id }}">Update</a></td> </tr> </tbody> {% endfor %} <tfoot> <tr> <th>username</th> <th>email</th> <th>Delete</th> <th>Update</th> </tr> </tfoot> </table> </div>
//user_up.html <form method="POST" action="/admincp/user/doup"> <input name="id" type="hidden" value="{{ user.id }}"> <p>Username:<input name="um" type="text" value="{{ user.username }}"></p> <p>Password:<input name="pwd" type="password" value="{{ user.password }}"></p> <p>Email:<input name="mail" type="text" value="{{ user.email }}"></p> <p> <button type="submit" value="Update"></button></p> </form>
8.在source--app--blog下新建form文件,創建form表單BlogForm.d、UserForm.d文件;與模板頁中name的字段值一致;并定義字段的范圍;
//BlogForm.d module app.blog.form.BlogForm; import hunt.framework; class BlogForm : Form { mixin MakeForm; string id; string uid; //與User表關聯的user_id @NotEmpty //name的值不能為空 string nm; @Length(6,18) //title的值在6到18位之間 string tit; @Length(10,100) //description的值在10到100位之間 string dept; string pic; //圖片 @Length(10,200) //content的值在10到200位之間 string cont; } //UserForm.d module app.blog.form.UserForm; import hunt.framework; class UserForm : Form { mixin MakeForm; string id; @Length(3, 18) //用戶名的值在3到18位之間 string um; @Length(6, 16) //密碼的值在6到16位之間 string pwd; @Length(6, 32) //郵箱的值在6到32位之間 string mail; }
9.進行Blog項目的前臺管理;確定前臺能夠實現的功能,在config下的routes中定義路由
GET /add blog.add //文章添加頁面 POST /blog/add blog.doadd //文章添加操作 GET /blog/delete blog.remove //文章刪除操作 GET /blog/list blog.blogs //文章列表瀏覽頁面 POST /blog/doup blog.doupdate //文章修改操作 GET /blog/update blog.update //文章修改頁面
10.在source--app--controller下創建BlogController.d文件,實現Blog前臺管理包括瀏覽,添加,修改,刪除功能
module app.blog.controller.BlogController; import app.blog.form.BlogForm; import app.blog.model.Blog; import app.blog.repository.BlogRepository; import hunt.framework; class BlogController : Controller { mixin MakeController; //文章添加 @Action string add()//跳轉到添加頁面添加 { return view.render("blog_add"); } @Action string doadd(BlogForm form)//添加操作 { auto blog = new Blog ; blog.title = form.tit; blog.description = form.dept; blog.picture = form.pic; blog.content = form.cont; blog.created = time();//時間為當前時間 auto repo = new BlogRepository; repo.save(blog);//保存 return "successed!"; } //文章列表 @Action string blogs() { auto repo = new BlogRepository; auto blogs = repo.findAll();//查找所有的文章信息 view.assign("blogs",blogs); return view.render("blog_list");//跳轉到列表頁面 } //文章刪除 @Action string remove(int id) { auto repo = new BlogRepository; repo.removeById(id);//通過id刪除文章信息 return "deleted"; } //文章修改 @Action string update(int id)//修改頁面 { auto repo = new BlogRepository; auto blog = repo.find(id);//通過id查到需要修改的文章信息 view.assign("blog",blog); return view.render("blog_up");//跳轉到修改頁面 } @Action string doupdate(BlogForm form)//修改操作 { auto repo = new BlogRepository; auto blog = repo.find(form.id.to!int); blog.title = form.tit; blog.description = form.dept; blog.picture = form.pic; blog.content = form.cont;//進行修改操作 repo.save(blog);//保存 return "successed"; } }
11.在views下的default中創建footer.html,header.html用來儲存模板頁中的CSS樣式,blog_add.html, blog_up.html,blog_list.html分別顯示blog添加,修改,列表模板頁;
//blog_list.html {% include "header.html" %} <!-- Page Content --> <div class="container"> <div class="row"> <!-- Blog Entries Column --> <div class="col-md-8"> <h2 class="my-4">Page Heading <small>Secondary Text</small> </h2> {% for blog in blogs %} <!-- Blog Post --> <div class="card mb-4"> <img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap"> <div class="card-body"> <h3 class="card-title">{{blog.title}}</h3> <p class="card-text">{{blog.description}}</p> <a href="/blog/view?id={{blog.id}}" class="btn btn-primary">Read More →</a> </div> <div class="card-footer text-muted"> Posted on {{ date("m-d H:i:s",blog.created)}} <a href="#">{{blog.user.username}}</a> </div> </div> {% endfor %} {% include "footer.html" %}
//blog_add.html <html> <body> <form method="POST" action="/blog/add"> <p>Name:<input name="nm" type="text"></p> <p>Title:<input name="tit" type="text"></p> <p>Description: <input name="dept" type="text"></p> <p>Picture: <input name="pic" type="text"></p> <p>Content: <input name="cont" type="text"></p> <p><button type="submit" value="Add"></button></p> </form> </body> </html>
//blog_up.html <form method="POST" action="/blog/doup"> <input name="id" type="hidden" value="{{blog.id}}"> <p>Name:<input name="nm" type="text" value="{{ blog.name }}"></p> <p>Title:<input name="tit" type="text" value="{{ blog.title }}"></p> <p>Description: <input name="dept" type="text" value="{{ blog.description }}"></p> <p>Picture: <input name="pic" type="text" value="{{ blog.picture }}"></p> <p>Content: <input name="cont" type="text" value="{{ blog.content }}"></p> <p><button type="submit" value="Update"></button></p> </form>
“D語言怎么創建一個blog項目”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。