Django(三)

1.http://django-chinese-docs.readthedocs.org/en/latest/intro/tutorial02.html

http://www.3wzhaopin.com

总结:几个关键性的文件:mysite/polls

admin.py

from django.contrib import admin
from polls.models import Poll
from polls.models import Choice

#admin.site.register(Poll)
class ChoiceInline(admin.TabularInline):
        model = Choice
        extra = 3

class PollAdmin(admin.ModelAdmin):
        list_display = ('question','pub_date')
        fieldsets = [
                (None, {'fields':['question']}),
                ('Date information', {'fields':['pub_date'], 'classes':['collap$
        ]
        inlines = [ChoiceInline]


admin.site.register(Poll, PollAdmin)
admin.site.register(Choice)
view.py

# Create your views here.
from django.http import HttpResponse
from polls.models import Poll
from django.template import Context, loader

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

def detail(request, poll_id):
        return HttpResponse("You're looking at poll %s." % poll_id)

def results(request, poll_id):
        return HttpResponse("You're looking at the results of poll %s." % poll_$
def vote(request, poll_id):
        return HttpResponse("You're voting on poll %s." % poll_id)
model.py

import datetime
from django.utils import timezone
from django.db import models

# Create your models here.
class Poll(models.Model):
        question = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
        def __unicode__(self):
                return self.question
        def was_published_recently(self):
                return self.pub_date >= timezone.now() - datetime.timedelta(day$
class Choice(models.Model):
        poll = models.ForeignKey(Poll)
        choice_text = models.CharField(max_length=200)
        votes = models.IntegerField(default=0)
        def __unicode__(self):
                return self.choice_text


urls.py

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
        url(r'^$', views.index, name='index'),
        url(r'^(?P<poll_id>\d+)/$',views.detail, name='detail'),
        url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
        url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)

简历templates文件假 polls文件夹 index.html

{% if latest_poll_list %}
        <ul>
        {% for poll in latest_poll_list %}
                <li><a href="/polls/{{poll.id}}/">{{poll.question}}</a></li>
        {% endfor %}
        </ul>
{% else %}
        <p>No polls are available. </p>
{% endif %}

下面是网站mysite,上面是应用

urls.py

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^mysite/', include('mysite.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    url(r'^polls/', include('polls.urls'))
)

setting.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'm$
        'NAME': '/root/opt/mycode/mysite/db',                      # Or path to$
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain s$
        'PORT': '',                      # Set to empty string for default.
    }
}

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'polls'
)

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/te$
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    '/root/opt/mycode/mysite/templates'
)


from django.shortcuts import render

from polls.models import Poll

def index(request):
    latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    context = {'latest_poll_list': latest_poll_list}
    return render(request, 'polls/index.html', context)

poll = get_object_or_404(Poll, pk=poll_id)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值