<file_sep>from django.contrib import admin

# Register your models here.


from .models import Questions, Choice

class QuestionAdmin(admin.ModelAdmin):
    list_display = ('question_text','pub_date','was_published_recently')
    #fields = ['question_text','pub_date']
    list_filter = ['pub_date']
    search_fields = ['question_text']
class ChoiceAdmin(admin.ModelAdmin):
    list_display = ('choice_text','votes')


admin.site.register(Questions,QuestionAdmin)
admin.site.register(Choice,ChoiceAdmin)


<file_sep># Python27
这个是Python2.7的项目相关
<file_sep><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<header>
    {% block header %}
    这个是main 页面，当这个页面出现时，说明被复用了。
    {% end %}
</header>
<footer>
{% block footer %}这个是main的footer 页面，当这个页面出现时，说明被复用了。  {% end %}
</footer>
</body>
</html><file_sep># -*- coding:utf-8 -*-
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.options
import os
import torndb

from tornado.options import options,define
define("port",default=8001,help="This is help",type=int)

class IndexGetHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html')

class IndexPostHandler(tornado.web.RequestHandler):
    conn = torndb.Connection("127.0.0.1", "users1", user="root", password="<PASSWORD>")
    @tornado.web.asynchronous
    def post(self):
        user_name = self.get_arguments('user_name')
        pass_word = self.get_argument('pass_word')
        str1 = 'abc'
        str1 = "SELECT password FROM users WHERE username= %s LIMIT 1"
        print self.conn.get(str1,user_name)
        get_password = self.conn.get(str1,user_name)
        if  get_password is not None:
            for name,value in get_password.items():
                print name,value,pass_word
                if value.strip()!='' and (str(pass_word) == str(value)):
                    for user in user_name:
                        self.redirect("/userinfolist")
                else:
                    self.redirect("/")
        else:
            self.redirect("/")
        self.conn.close()

class UserInfoGetHandler(tornado.web.RequestHandler):
    conn = torndb.Connection("127.0.0.1", "users1", user="root", password="<PASSWORD>")
    def get(self):
       str1 = 'abc'
       str1 = "SELECT username,password FROM users"
       get_user_passwd = self.conn.query(str1)
       print get_user_passwd
       if get_user_passwd is not None:
           self.render('result.html',get_user_passwd=get_user_passwd)
       self.conn.close()

    def write_error(self, status_code, **kwargs):
        self.write("G<NAME>it, user! You caused a %d error." % status_code)

if __name__ == '__main__':
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        handlers=[(r"/",IndexGetHandler),(r"/result",IndexPostHandler),(r"/userinfolist",UserInfoGetHandler)],
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        debug=True
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop().instance().start()


<file_sep><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>
    {% block ti %}
        {{ year }}
    {% endblock %}
</title>
    </head>
<body>

{% block question_text %}

{% for y in year_list %}

{{ y.question_text }}

{% endfor %}

<p>{{ year }}</p>

{% endblock %}



{% block pub_date %}

{% for y in year_list %}

{{ y.pub_date | date:"F j,Y" }}

{% endfor %}

<p>{{ year }}</p>

{% endblock %}



</body>
</html>

====================================
<file_sep>"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
#from polls.views import index as poll_views_index
from polls.views import detail as poll_views_detail
from polls.views import results as poll_views_result
from polls.views import vote as poll_views_vote
from polls.views import year_archive as poll_views_year_archive
from polls.views import os_command as poll_views_os_command
from polls.views import DetailView,IndexView

urlpatterns = [
    url(r'^', admin.site.urls),
    url(r'^(?P<parameter1>[0-9]+)/vote/$',poll_views_vote,name='poll_views_vote'),
    url(r'^year_archive/(?P<year>[0-9]+)/$', poll_views_year_archive, name='poll_views_year_archive'),

    url(r'^detail/(?P<pk>[0-9]+)/$', DetailView.as_view(), name='detail'),
    url(r'^index/(?P<pk>[0-9]+)/$', IndexView.as_view(), name='index'),

   # url(r'^detail/(?P<question_id>[0-9]+)/$',poll_views_detail,name='poll_views_detail'),
    url(r'^results/(?P<question_id>[0-9]+)/$', poll_views_result, name='poll_views_result'),
    url(r'^vote/(?P<question_id>[0-9]+)/$', poll_views_vote, name='poll_views_vote'),
    #url(r'^index/$', poll_views_index, name='poll_views_index'),
    url(r'os_command/$',poll_views_os_command,name='os_command'),


]
<file_sep># -*- coding: utf-8 -*-

from django.shortcuts import render,get_list_or_404,get_object_or_404
from django.http import Http404
from django.http import HttpResponse

from django.views import generic

# Create your views here.

from .models import Questions, Choice

def vote(request,parameter1):
    try:
        question = get_list_or_404(Questions, pk=parameter1)
        print question
    except Questions.DoesNotExist:
        raise Http404("Question does not exist")
    context = {'question':question}
    return render(request,'polls/vote.html',context)

def year_archive(request,year):
    try:
        year_list = Questions.objects.filter(pub_date__year= year)
        print year_list
    except Questions.DoesNotExist:
        raise Http404("Question does not exist")
    context = {'year':year,'year_list':year_list}
    return render(request,'polls/year_archive.html',context)

def detail(request,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)

# def index(request):
#     last_question_list = Questions.objects.order_by('pub_date')[:10]
#    # output = '|'.join([q.question_text for q in last_question_list])
#     #context = {'output':output}
#     return render(request,'polls/index.html',{'last_question_list':last_question_list})


class IndexView(generic.ListView):
    #model = Questions
    context_object_name = 'latest_question_list'
    template_name = 'polls/index.html'   #默认的时候questions_list 作为使用的template中变量使用，可以使用context_object_name自定义变量名称
    def get_queryset(self):
        return Questions.objects.order_by('-pub_date')[:10]

class DetailView(generic.DetailView):
    model = Questions
    template_name = 'polls/detail.html'

class ResultView(generic.DetailView):
    model = Questions
    template_name = 'polls/result.html'

# Python 调用系统命令
def os_command(request):
    # import os
    # context = os.system('dir d:')
    # print context
    # return HttpResponse( context)
    a = 10
    b = 2
    c = a / b
    print ("a / b = {0}{1}".format(c,"ok"))
    return HttpResponse(c)

