Django框架学习(二)

管理界面开启

1
python manage.py createsuperuser
1
2
# 开启中文 settings.py
LANGUAGE_CODE = 'zh-Hans'

管理界面添加应用

1
2
3
from django.contrib import admin
from .models import Question
admin.site.register(Question)

应用接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# polls/views.py
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {
'latest_question_list': latest_question_list
}
return render(request, 'polls/index.html', context)


def detail(request, id):
# 尝试用 get() 函数获取一个对象,如果不存在就抛出 Http404 错误
question = get_object_or_404(Question, pk=id)
# 类似于java的thymeleaf模版的model,将对象显示到detail.html中
return render(request, 'polls/detail.html', {'question': question})
赏个🍗吧
0%