翻译进度
2
分块数量
2
参与人数

Django Admin 管理后台

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

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


我们将使用 Django admin 添加,编辑和删除我们刚刚创建的帖子。

让我们在代码编辑器打开 blog/admin.py 文件,并替换其中的内容如下:

from django.contrib import admin
from .models import Post

admin.site.register(Post)

如你所见,我们导入(包括)了前一章定义的 Post 模型。 为了让我们的模型在 admin 页面上可见,我们需要使用 admin.site.register(Post) 来注册模型.

OK, 现在来看看我们的 Post 模型。 记得先在控制台输入
python manage.py runserver 启动服务器。 然后打开浏览器,输入地址 127.0.0.1:8000/admin/ 你会看到像这样的登录界面:

Login page

为了登录,你需要创建一个掌控整个网站所有东西的超级用户。 回到刚才的命令行,输入 python manage.py createsuperuser,按下 Enter。

记住,为了在 web 服务器运行的时候写命令,需要打开一个新的终端窗口并激活你的虚拟环境。我们可以在 你的第一个Django 项目! 章中的启动 web 服务器 部分回顾如何写命令。

(myvenv) ~/djangogirls$ python manage.py createsuperuser
(myvenv) C:\Users\Name\djangogirls> python manage.py createsuperuser
123wula 翻译于 2周前

When prompted, type your username (lowercase, no spaces), email address, and password. Don't worry that you can't see the password you're typing in – that's how it's supposed to be. Type it in and press enter to continue. The output should look like this (where the username and email should be your own ones):

Username: ola
Email address: ola@example.com
Password:
Password (again):
Superuser created successfully.

Return to your browser. Log in with the superuser's credentials you chose; you should see the Django admin dashboard.

Django admin

Go to Posts and experiment a little bit with it. Add five or six blog posts. Don't worry about the content –- it's only visible to you on your local computer -- you can copy-paste some text from this tutorial to save time. :)

Make sure that at least two or three posts (but not all) have the publish date set. It will be helpful later.

Django admin

If you want to know more about Django admin, you should check Django's documentation: https://docs.djangoproject.com/en/2.2/ref/...

This is probably a good moment to grab a coffee (or tea) or something to eat to re-energize yourself. You created your first Django model – you deserve a little break!

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

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

Summer
贡献者:2
讨论数量: 0
发起讨论 查看所有版本


暂无话题~