--- title: 'Assessment: DIY Django mini blog' slug: Learn/Server-side/Django/django_assessment_blog tags: - django - 初學者 - 部落格 translation_of: Learn/Server-side/Django/django_assessment_blog ---
在這個評估中,您將使用您在 Django Web Framework (Python) 模組中獲得的知識,來創建一個非常基本的部落格。
前提: |
在開始時做這章節的任務之前,你應該已經看完這個模組的所有文章了。 |
---|---|
目標: |
測試Django基礎的綜合應用,包含URL設定、模型、視圖、表單和模板。 |
需要顯示的頁面與對應的URLs和需求提列於下表:
頁面 | URL | 需求 |
---|---|---|
首頁 | / 和 /blog/ |
關於此站的說明。 |
所有部落格文章的清單 | /blog/blogs/ |
所有部落格文章的清單。
|
部落格作者(blogger) 詳細頁面 | /blog/blogger/<author-id> |
特定作者(由id指定)的資訊與他所發布的部落格文章。
|
部落格文章詳細頁面 | /blog/<blog-id> |
部落格文章詳細內容。
|
部落格作者清單 | /blog/bloggers/ |
系統內的部落格作者清單。
|
回覆表單頁 | /blog/<blog-id>/create |
新增回覆於特定文章。
|
使用者身分認證頁 | /accounts/<standard urls> |
標準的Django身分驗證頁面,用來登入、登出及修改密碼。
|
管理者網頁 | /admin/<standard urls> |
管理者網頁必須能新增/編輯/刪除部落格文章、作者及回覆。
|
另外您應該要寫一些基本的測試來驗證:
__str__()
回傳期望的值)。get_absolute_url()
回傳期望的URL)。Note: 當然你也可以跑很多其他的測試。但是我們會希望您至少實作以上列出的測試項目。
下一區塊顯示符合以上需求的網頁截圖。
The following screenshot provide an example of what the finished program should output.
這個頁面會列出所有部落格內的文章(可以從側邊選單的“所有文章”連結進入)。
幾項提醒:
可以由側邊選單的“所有部落客”進入此頁面,並於頁面上提供連結至每一位部落客。
從截圖可以發現到,並沒有任何一位使用者登入。
顯示某篇特定部落格文章的詳細內容。
請注意每個評論都有日期與時間,並且由最後至最新排列(與部落格文章相反)。
我們可以看見最底下有個連結連到新增評論的表單。當使用者沒有登入時,我們改以要求登入的連結代替。
這張表單用來新增評論,且使用者必須是登入狀態。當表單送出成功之後,我們必須回到相對應的部落格文章內容頁。
這頁顯示部落客的介紹資料以及列出他們所發表的部落格文章。
以下說明實作的步驟。
This project is very similar to the LocalLibrary tutorial. You will be able to set up the skeleton, user login/logout behaviour, support for static files, views, URLs, forms, base templates and admin site configuration using almost all the same approaches.
Some general hints:
get_queryset(self)
to do the filtering (much like in our library class LoanedBooksAllListView
) and get the author information from the URL.get_context_data()
(discussed below).CreateView
. If you use a CreateView
(recommended) then:
get_context_data()
as discussed below). 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)
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.We briefly talked about passing a context to the template in a class-based view in the Django Tutorial Part 6: Generic list and detail views topic. To do this you need to override get_context_data()
(first getting the existing context, updating it with whatever additional variables you want to pass to the template, and then returning the updated context). For example, the code fragment below shows how you can add a blogger object to the context based on their 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
The assessment for this task is available on Github here. This assessment is primarily based on how well your application meets the requirements we listed above, though there are some parts of the assessment that check your code uses appropriate models, and that you have written at least some test code. When you're done, you can check out our the finished example which reflects a "full marks" project.
Once you've completed this module you've also finished all the MDN content for learning basic Django server-side website programming! We hope you enjoyed this module and feel you have a good grasp of the basics!
{{PreviousMenu("Learn/Server-side/Django/web_application_security", "Learn/Server-side/Django")}}