翻译进度
1
分块数量
0
参与人数

Django 视图

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。

《Django Girls》的 中文翻译 1.8 版本 已经翻译完成,请在参考 1.8 版本的基础上进行校正和翻译。


Time to get rid of the bug we created in the last chapter! :)

view is a place where we put the "logic" of our application. It will request information from the model you created before and pass it to a template. We'll create a template in the next chapter. Views are just Python functions that are a little bit more complicated than the ones we wrote in the Introduction to Python chapter.

Views are placed in the views.py file. We will add our views to the blog/views.py file.

blog/views.py

OK, let's open up this file in our code editor and see what's in there:

from django.shortcuts import render

# Create your views here.

Not too much stuff here yet.

Remember that lines starting with # are comments – this means that those lines won't be run by Python.

Let's create a view as the comment suggests. Add the following minimal view below it:

def post_list(request):
    return render(request, 'blog/post_list.html', {})

As you can see, we created a function (def) called post_list that takes request and will return the value it gets from calling another function render that will render (put together) our template blog/post_list.html.

Save the file, go to http://127.0.0.1:8000/ and see what we've got.

Another error! Read what's going on now:

Error

This shows that the server is running again, at least, but it still doesn't look right, does it? Don't worry, it's just an error page, nothing to be scared of! Just like the error messages in the console, these are actually pretty useful. You can read that the TemplateDoesNotExist. Let's fix this bug and create a template in the next chapter!

Learn more about Django views by reading the official documentation: https://docs.djangoproject.com/en/2.2/topi...

本文章首发在 LearnKu.com 网站上。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

Summer
讨论数量: 0
发起讨论 只看当前版本


暂无话题~