|
|
|
@ -5,47 +5,67 @@ from django.http import Http404 |
|
|
|
|
from .models import Choice, Question |
|
|
|
|
from django.template import loader |
|
|
|
|
from django.urls import reverse |
|
|
|
|
from django.views import generic |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create your views here. |
|
|
|
|
|
|
|
|
|
def index(request): |
|
|
|
|
latest_question_list = Question.objects.order_by('-pub_date')[:5] |
|
|
|
|
# output = ', '.join([q.question_text for q in latest_question_list]) |
|
|
|
|
# return HttpResponse(output) |
|
|
|
|
# 模板使用方式1 |
|
|
|
|
# template = loader.get_template('index.html') |
|
|
|
|
# context = { |
|
|
|
|
# 'latest_question_list': latest_question_list, |
|
|
|
|
# } |
|
|
|
|
# return HttpResponse(template.render(context, request)) |
|
|
|
|
# 模板使用方法2 |
|
|
|
|
context = { |
|
|
|
|
'latest_question_list': latest_question_list, |
|
|
|
|
} |
|
|
|
|
return render(request, 'index.html', context) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def detail(request, question_id): |
|
|
|
|
# return HttpResponse(f"You're looking at question {question_id}.") |
|
|
|
|
# 添加404页面展示 方式1 |
|
|
|
|
# try: |
|
|
|
|
# question = Question.objects.get(pk=question_id) |
|
|
|
|
# context = {'question': question} |
|
|
|
|
# print(context) |
|
|
|
|
# except Question.DoesNotExist: |
|
|
|
|
# raise Http404("Question does not exist") |
|
|
|
|
# return render(request, 'detail.html', context) |
|
|
|
|
|
|
|
|
|
# 添加404页面展示 方式2 |
|
|
|
|
question = get_object_or_404(Question, pk=question_id) |
|
|
|
|
return render(request, 'detail.html', {'question': question}) |
|
|
|
|
# def index(request): |
|
|
|
|
# latest_question_list = Question.objects.order_by('-pub_date')[:5] |
|
|
|
|
# # output = ', '.join([q.question_text for q in latest_question_list]) |
|
|
|
|
# # return HttpResponse(output) |
|
|
|
|
# # 模板使用方式1 |
|
|
|
|
# # template = loader.get_template('index.html') |
|
|
|
|
# # context = { |
|
|
|
|
# # 'latest_question_list': latest_question_list, |
|
|
|
|
# # } |
|
|
|
|
# # return HttpResponse(template.render(context, request)) |
|
|
|
|
# # 模板使用方法2 |
|
|
|
|
# context = { |
|
|
|
|
# 'latest_question_list': latest_question_list, |
|
|
|
|
# } |
|
|
|
|
# return render(request, 'index.html', context) |
|
|
|
|
# |
|
|
|
|
# |
|
|
|
|
# def detail(request, question_id): |
|
|
|
|
# # return HttpResponse(f"You're looking at question {question_id}.") |
|
|
|
|
# # 添加404页面展示 方式1 |
|
|
|
|
# # try: |
|
|
|
|
# # question = Question.objects.get(pk=question_id) |
|
|
|
|
# # context = {'question': question} |
|
|
|
|
# # print(context) |
|
|
|
|
# # except Question.DoesNotExist: |
|
|
|
|
# # raise Http404("Question does not exist") |
|
|
|
|
# # return render(request, 'detail.html', context) |
|
|
|
|
# |
|
|
|
|
# # 添加404页面展示 方式2 |
|
|
|
|
# question = get_object_or_404(Question, pk=question_id) |
|
|
|
|
# return render(request, 'detail.html', {'question': question}) |
|
|
|
|
# |
|
|
|
|
# |
|
|
|
|
# def result(request, question_id): |
|
|
|
|
# question = get_object_or_404(Question, pk=question_id) |
|
|
|
|
# return render(request, 'results.html', {'question': question}) |
|
|
|
|
# # return HttpResponse(f"You're looking at the result of question {question_id}.") |
|
|
|
|
|
|
|
|
|
# 通用视图 |
|
|
|
|
|
|
|
|
|
def result(request, question_id): |
|
|
|
|
question = get_object_or_404(Question, pk=question_id) |
|
|
|
|
return render(request, 'results.html', {'question': question}) |
|
|
|
|
# return HttpResponse(f"You're looking at the result of question {question_id}.") |
|
|
|
|
class IndexView(generic.ListView): |
|
|
|
|
template_name = 'index.html' |
|
|
|
|
context_object_name = 'latest_question_list' |
|
|
|
|
|
|
|
|
|
def get_queryset(self): |
|
|
|
|
return Question.objects.order_by('-pub_date')[:5] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DetailView(generic.DetailView): |
|
|
|
|
model = Question |
|
|
|
|
template_name = 'detail.html' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResultView(generic.DetailView): |
|
|
|
|
model = Question |
|
|
|
|
template_name = 'results.html' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def vote(request, question_id): |
|
|
|
|