Python Console(2)Vesion Upgrade and Sample Admin/List/Detail

本文详细介绍了如何使用Django创建超级用户,设置模型管理界面,并通过视图函数实现投票应用的各项功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python Console(2)Vesion Upgrade and Sample Admin/List/Detail

Creating an admin user
>python manage.py createsuperuser

my default user name is carl, and my usually password.

Make the poll app modifiable in the admin.
modify the polls/admin.py file
# -*- coding: utf-8 -*-from __future__ import unicode_literals

from django.contrib import admin

# Register your models here.from .models import Question Admin
from .models import Choice

admin.site.register(Question)
admin.site.register(Choice)

View Module and Functions
Question ‘index’ Page, Question ‘detail’ page, Question ‘results’ page, Vote action

prepare more views in polls/views.py

def detail(requesst, question_id):
return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
response = "You're looking at the results of question %s." return HttpResponse(response % question_id)

def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)

url mapping in polls/urls.py
urlpatterns = [
# ex: /polls/ url(r'^$', views.index, name='index'),
# ex: /polls/5/ url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/ url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name="results"),
# ex: /polls/5/vote/ url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name="vote"),
]

Call the DAO API and doing something in the index page
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)

Use Template
django will look for templates directory in default. polls/templates/polls/index.html
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li>
<a href="/polls/{{question.id}}/">
{{ question.question_text }}
</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif%}

The template render and loading
from django.http import HttpResponse
from django.template import loader
from .models import Question


def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {
'latest_question_list':latest_question_list,
}
return HttpResponse(template.render(context,request))

We can use shortcut render instead of template loader
from django.shortcuts import render
from django.http import HttpResponse
from .models import Question


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)


Raising a 404 if necessary
from django.http import Http404
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, 'polls/detail.html', { 'question': question})

A shortcut: get_object_or_404()
from django.shortcuts import get_object_or_404, render
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', { 'question': question})

In side the template we can get the URL from configuration
<li>
<a href="{% url 'detail' question.id %}">
{{ question.question_text }}
</a>
</li>


References:
https://docs.djangoproject.com/en/1.11/intro/tutorial02/
https://docs.djangoproject.com/en/1.11/intro/tutorial03/
https://docs.djangoproject.com/en/1.11/intro/tutorial04/
https://docs.djangoproject.com/en/1.11/intro/tutorial05/
https://docs.djangoproject.com/en/1.11/intro/tutorial06/
https://docs.djangoproject.com/en/1.11/intro/tutorial07/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值