本篇内容介绍了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。