Assessment: DIY Django mini blog

2018-05-15 17:26 更新
先決條件: 在嘗試此評估之前,您應(yīng)該已經(jīng)完成了本單元中的所有文章。
目的: 測試對Django基礎(chǔ)知識的理解,包括URL配置,模型,視圖,表單和模板。

工程概要

需要顯示的頁面,其URL和其他要求如下所示:

網(wǎng)址 要求
主頁 / and /blog/ 描述網(wǎng)站的索引頁。
所有博文的列表 /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/

博客系統(tǒng)上的列表:

  • Accessible to all users from site sidebar
  • Blogger names are linked to Blog author detail pages.
評論表單頁 /blog/<blog-id>/create

創(chuàng)建博客評論:

  • 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>

用于登錄,注銷和設(shè)置密碼的標準Django身份驗證頁面:

  • Login/out should be accessible via sidebar links.
管理網(wǎng)站 /admin/<standard urls>

應(yīng)啟用管理網(wǎng)站,允許創(chuàng)建/編輯/刪除博客帖子,博客作者和博客評論(這是博客創(chuàng)建新博客帖子的機制):

  • 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.

此外,你應(yīng)該編寫一些基本測試來驗證:

  • 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)

注意:當然還有許多其他測試可以運行。 使用您的自由裁量權(quán),但我們希望您至少進行上述測試。

以下部分顯示了實施上述要求的網(wǎng)站的屏幕截圖

屏幕截圖

以下屏幕截圖提供了已完成程序應(yīng)輸出的示例。

所有博文的列表

這會顯示所有博客帖子的列表(可從側(cè)邊欄中的"所有博客"鏈接訪問)。 注意事項:

  • 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;">

所有博客的列表

這提供了指向所有博客作者的鏈接,鏈接來自側(cè)邊欄中的"所有博主"鏈接。 在這種情況下,我們可以從側(cè)邊欄看到?jīng)]有用戶登錄。

; width:493px;">

博客詳細信息頁面

這顯示了特定博客的詳細信息頁面。

; width:986px;">

請注意,評論有一個日期時間,并且從最舊到最新(與博客排序相反)排序。 最后,我們有一個鏈接,用于訪問表單以添加新的評論。 如果用戶未登錄,我們會看到一個建議要登錄。

; width:646px;">

添加評論表單

這是添加注釋的形式。 注意,我們已經(jīng)登錄。當這個成功,我們應(yīng)該回到相關(guān)的博客帖子頁面。

; 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 教程非常相似。 您將能夠使用幾乎所有相同的方法設(shè)置框架,用戶登錄/注銷行為,支持靜態(tài)文件,視圖,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()(首先獲取現(xiàn)有的上下文,用你想要傳遞給模板的其他變量更新它,然后返回更新的上下文。例如, 下面的片段顯示如何根據(jù)他們的 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

評定

此任務(wù)的評估是在Github此處。 此評估主要基于您的應(yīng)用程序滿足上述要求的程度,盡管有一些評估部分檢查您的代碼是否使用適當?shù)哪P?,以及您是否至少編寫了一些測試代碼。 完成后,您可以查看我們的完成示例,其中反映了"完整 標記"項目。

完成本單元后,您還完成了學習基本Django服務(wù)器端網(wǎng)站編程的所有MDN內(nèi)容! 我們希望你喜歡這個模塊,并覺得你有一個很好的基礎(chǔ)知識!

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號