codecamp

Assessment: DIY Django mini blog

先决条件: 在尝试此评估之前,您应该已经完成了本单元中的所有文章。
目的: 测试对Django基础知识的理解,包括URL配置,模型,视图,表单和模板。

工程概要

需要显示的页面,其URL和其他要求如下所示:

网址 要求
主页 / and /blog/ 描述网站的索引页。
所有博文的列表 /blog/blogs/

所有博文的列表:

  • Accessible to all users from a sidebar link.
  • List sorted by post date (newest to oldest).
  • List paginated in groups of 5 articles.
  • List items display the blog title, post date, and author.
  • Blog post names are linked to blog detail pages.
  • Blogger (author names) are linked to blog author detail pages.
博客作者(博客)详细页面 /blog/blogger/<author-id>

指定作者的信息(按id)及其博客文章列表:

  • Accessible to all users from author links in blog posts etc.
  • Contains some biographical information about the blogger/author.
  • List sorted by post date (newest to oldest).
  • Not paginated.
  • List items display just the blog post name and post date.
  • Blog post names are linked to blog detail pages.
博客帖子详细信息页面 /blog/<blog-id>

博客文章。

  • Accessible to all users from blog post lists.
  • Page contains the blog post: name, author, post date, and content.
  • Comments for the blog post should be displayed at bottom.
  • Comments should be sorted in order: oldest to most recent.
  • Contains link to add comments at end for logged in users (see Comment form page)
  • Blog posts and comments need only display plain text. There is no need to support any sort of HTML markup (e.g. links, images, bold/italic, etc).
所有博客的列表 /blog/bloggers/

博客系统上的列表:

  • Accessible to all users from site sidebar
  • Blogger names are linked to Blog author detail pages.
评论表单页 /blog/<blog-id>/create

创建博客评论:

  • Accessible to logged-in users (only) from link at bottom of blog post detail pages.
  • Displays form with description for entering comments (post date and blog is not editable).
  • After a comment has been posted, the page will redirect back to the associated blog post page.
  • Users cannot edit or delete their posts.
  • Logged out users will be directed to the login page to log in, before they can add comments. After logging in, they will be redirected back to the blog page they wanted to comment on.
  • Comment pages should include the name/link to the blogpost being commented on.
用户验证页面 /accounts/<standard urls>

用于登录,注销和设置密码的标准Django身份验证页面:

  • Login/out should be accessible via sidebar links.
管理网站 /admin/<standard urls>

应启用管理网站,允许创建/编辑/删除博客帖子,博客作者和博客评论(这是博客创建新博客帖子的机制):

  • Admin site blog posts records should display the list of associated comments inline (below each blog post).
  • Comment names in the Admin site are created by truncating the comment description to 75 characters.
  • Other types of records can use basic registration.

此外,你应该编写一些基本测试来验证:

  • All model fields have the correct label and length.
  • All models have the expected object name (e.g. __str__() returns the expected value).
  • Models have the expected URL for individual Blog and Comment records (e.g. get_absolute_url() returns the expected URL).
  • The BlogListView (all-blog page) is accessible at the expected location (e.g. /blog/blogs)
  • The BlogListView (all-blog page) is accessible at the expected named url (e.g. 'blogs')
  • The BlogListView (all-blog page) uses the expected template (e.g. the default)
  • The BlogListView paginates records by 5 (at least on the first page)

注意:当然还有许多其他测试可以运行。 使用您的自由裁量权,但我们希望您至少进行上述测试。

以下部分显示了实施上述要求的网站的屏幕截图

屏幕截图

以下屏幕截图提供了已完成程序应输出的示例。

所有博文的列表

这会显示所有博客帖子的列表(可从侧边栏中的"所有博客"链接访问)。 注意事项:

  • The sidebar also lists the logged in user.
  • Individual blog posts and bloggers are accessible as links in the page.
  • Pagination is enabled (in groups of 5)
  • Ordering is newest to oldest.

; width:986px;">

所有博客的列表

这提供了指向所有博客作者的链接,链接来自侧边栏中的"所有博主"链接。 在这种情况下,我们可以从侧边栏看到没有用户登录。

; width:493px;">

博客详细信息页面

这显示了特定博客的详细信息页面。

; width:986px;">

请注意,评论有一个日期时间,并且从最旧到最新(与博客排序相反)排序。 最后,我们有一个链接,用于访问表单以添加新的评论。 如果用户未登录,我们会看到一个建议要登录。

; width:646px;">

添加评论表单

这是添加注释的形式。 注意,我们已经登录。当这个成功,我们应该回到相关的博客帖子页面。

; width:778px;">

作者简介

这会显示博客的生物信息及其博客帖子列表。

; width:982px;">

步骤完成

以下部分描述了您需要做什么。

  1. Create a skeleton project and web application for the site (as described in Django Tutorial Part 2: Creating a skeleton website). You might use 'diyblog' for the project name and 'blog' for the application name.
  2. Create models for the Blog posts, Comments, and any other objects needed. When thinking about your design, remember:
    • Each comment will have only one blog, but a blog may have many comments.
    • Blog posts and comments must be sorted by post date.
    • Not every user will necessarily be a blog author though any user may be a commenter.
    • Blog authors must also include bio information.
  3. Run migrations for your new models and create a superuser.
  4. Use the admin site to create some example blog posts and blog comments.
  5. Create views, templates, and URL configurations for blog post and blogger list pages.
  6. Create views, templates, and URL configurations for blog post and blogger detail pages.
  7. Create a page with a form for adding new comments (remember to make this only available to logged in users!)

提示和提示

此项目与 LocalLibrary 教程非常相似。 您将能够使用几乎所有相同的方法设置框架,用户登录/注销行为,支持静态文件,视图,URL,表单,基本模板和管理站点配置。

一些一般提示:

  1. The index page can be implemented as a basic function view and template (just like for the locallibrary).
  2. The list view for blog posts and bloggers, and the detail view for blog posts can be created using the generic list and detail views.
  3. The list of blog posts for a particular author can be created by using a generic list Blog list view and filtering for blog object that match the specified author.
    • You will have to implement get_queryset(self) to do the filtering (much like in our library class LoanedBooksAllListView) and get the author information from the URL.
    • You will also need to pass the name of the author to the page in the context. To do this in a class-based view you need to implement get_context_data() (discussed below).
  4. The add comment form can be created using a function-based view (and associated model and form) or using a generic CreateView. If you use a CreateView (recommended) then:
    • You will also need to pass the name of the blog post to the comment page in the context (implement get_context_data() as discussed below).
    • The form should only display the comment "description" for user entry (date and associated blog post should not be editable). Since they won't be in the form itself, your code will need to set the comment's author in the form_valid() function so it can be saved into the model (as described here — Django docs). In that same function we set the associated blog. A possible implementation is shown below (pk is a blog id passed in from the URL/URL configuration).
          def form_valid(self, form):
              """
              Add author and associated blog to form data before setting it as valid (so it is saved to model)
              """
              #Add logged-in user as author of comment
              form.instance.author = self.request.user
              #Associate comment with blog based on passed id
              form.instance.blog=get_object_or_404(Blog, pk = self.kwargs['pk'])
              # Call super-class form validation behaviour
              return super(BlogCommentCreate, self).form_valid(form)
      
    • You will need to provide a success URL to redirect to after the form validates; this should be the original blog. To do this you will need to override get_success_url() and "reverse" the URL for the original blog. You can get the required blog ID using the self.kwargs attribute, as shown in the form_valid() method above.

我们简要介绍了在 -based_views"> Django教程第6部分:通用列表和详细信息视图主题。 要做到这一点你需要重写 get_queryset()(首先获取现有的上下文,用你想要传递给模板的其他变量更新它,然后返回更新的上下文。例如, 下面的片段显示如何根据他们的 BlogAuthor id将博主对象添加到上下文中。

class SomeView(generic.ListView):
    ...  
        
    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(SomeView, self).get_context_data(**kwargs)
        # Get the blogger object from the "pk" URL parameter and add it to the context
        context['blogger'] = get_object_or_404(BlogAuthor, pk = self.kwargs['pk'])
        return context

评定

此任务的评估是在Github此处 此评估主要基于您的应用程序满足上述要求的程度,尽管有一些评估部分检查您的代码是否使用适当的模型,以及您是否至少编写了一些测试代码。 完成后,您可以查看我们的完成示例,其中反映了"完整 标记"项目。

完成本单元后,您还完成了学习基本Django服务器端网站编程的所有MDN内容! 我们希望你喜欢这个模块,并觉得你有一个很好的基础知识!

Django Tutorial Part 3: Using models
Django Tutorial Part 4: Django admin site
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录
CSS

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }