django文檔

django-admin startproejct mysite
cd mysite
pyton manage.py runserver
瀏覽器訪問:http://127.0.0.1:8000
python manage.py startapp polls
編輯polls/views.py
====
from django.http import HttpResponse


def index(request):
    return HttpResponse('This is polls home page.')
====|
新建polls/urls.py
====
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index,  name='index'),
]
====|
編輯mysite/urls.py
====
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]
====|
python manage.py runserver 80001
瀏覽器訪問:http://127.0.0.1:8001
==================================================================================================================================
了解mysite/settings.py
DATABASES:
    ENGINE:'django.db.backends.sqlite3','django.db.backends.mysql','django.db.backends.postgresql','django.db.backends.oracle'
INSTALLED_APPS:
    django.contrib.admin         - 管理員站點(diǎn)
    django.contrib.auth      - 認(rèn)證授權(quán)系統(tǒng)
    django.contrib.contenttypes  - 內(nèi)容類型框架
    django.contrib.sessions      - 會話框架
    django.contrib.messages      - 消息框架
    django.contrib.staticfiles   - 管理靜態(tài)文件的框架
python manage.py migrate
編輯polls/models.py
====
from django.db import models
from django.utils import timezone


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date =  models.DateTimeField('date pulished')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    vote = models.IntegerField(default=0)
====|
編輯mysite/settings.py
====
INSTALLED_APPS = ['polls.apps.PollsConfig']
====|
python manage.py makemigrations polls
python manage.py sqlmigrate polls 0001
python manage.py migrate
python manage.pyt shell
from polls.models import Question, Choice
from django.utils import timezone
Question.objects.all()
q = Question(question_text='what is up?', pub_date=timezone.now())
q.save()
Question.objects.all()
編輯polls/models.py
====
from django.db import models
from django.utils import timezone


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text
====|
python manage.py shell
from polls.models import Question, Choice
Question.objects.all()
編輯polls/models.py
====
import datetime
from django.db import models
from django.utils import timezone

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date pulished')

    def was_pulished_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

    def __str__(self):
        return self.question_text


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text
====|
python manage.py shell
from polls.models import Question, Choice
q = Question.objects.get(pk=1)
q.question_text
q.pub_date
q.was_pulished_recently()
from django.utils import timezone
current_year = timezone.now().year
Question.objects.get(pub_date__year=current_year)
q.choice_set.all()
q.choice_set.create(choice_text='Not much', votes=0)
q.choice_set.create(choice_text='The sky', votes=0)
c = q.choice_set.create(choice_text='Just hacking again', votes=0)
c.question
q.choice_set.all()
q.choice_set.couter()
Choice.objects.filter(question__pub_date__year=current_year)
c = q.choice_set.filter(choice_text__startswith='Just hacking')
c.delete()
python manage.py createsuperuser
python manage.py runserver
編輯polls/admin.py
====
from django.contriab import admin
from .models import Question, Choice


admin.site.register(Question)
admin.site.register(Choice)
====|
python manage.py runserver
==================================================================================================================================
編輯polls/views.py
====
from django.http import HttpRespone


def index(request):
    return HttpResponse('This is a index page.')

def detail(request, question_id):
    return HttpResponse('Your are looking at detail Question %s.'%question_id)

def results(request, question_id):
    return HttpResponse('Your are looking at results Question %s.'%question_id)

def vote(request, question_id):
    return HttpResponse('Your are looking at vote Question %s.'%question_id)
====|
編輯polls/urls.py
====
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('<int:question_id>/', views.detail, name='detail'),
    path('<int:question_id>/results', views.results, name='results'),
    path('<int:question_id>/vote', views.vote, name='vote')
]
====|
python manage.py runserver
瀏覽器:http://127.0.0.1:8000/polls/
瀏覽器:http://127.0.0.1:8000/polls/1
瀏覽器:http://127.0.0.1:8000/polls/1/results
瀏覽器:http://127.0.0.1:8000/polls/1/vote
編輯polls/views.py
====
from django.http import HttpResponse
from .models import Question


def index(request):
    last_question_list = Question.objects.order_by('-pub_date')[:5]
    output = ','.join([q.question_text for q in last_question_list])
    return HttpResponse(output)

def detail(request, question_id):
    return HttpResponse('Your are looking at detail Question %s.'%question_id)

def results(request, question_id):
    return HttpResponse('Your are looking at results Question %s.'%question_id)

def vote(request, question_id):
    return HttpResponse('Your are looking at vote Question %s.'%question_id)
====|
python manage.py runserver
瀏覽器:http://127.0.0.1:8000/polls/
編輯polls/views.py
====
from django.http import HttpResponse
from django.template import loader
from .models import Question


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

def detail(request, question_id):
    return HttpResponse('Your are looking at detail Question %s.'%question_id)

def results(request, question_id):
    return HttpResponse('Your are looking at results Question %s.'%question_id)

def vote(request, question_id):
    return HttpResponse('Your are looking at vote Question %s.'%question_id)
====|
新建polls/templates/polls/index.html
====
{% if last_question_list %}
    <ul>
        {% for question in last_question_list %}
            <li><a href='/polls/{{ question.id }}/'>{{ question.question_text}}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls.</p>
{% endif %}
====|
python manage.py runserver
瀏覽器:http://127.0.0.1:8000/polls/
編輯polls/views.py
====
from django.shortcuts import render
from .models import Question
from django.http import HttpReponse


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

def detail(request, question_id):
    return HttpResponse('Your are looking at detail Question %s.'%question_id)

def results(request, question_id):
    return HttpResponse('Your are looking at results Question %s.'%question_id)

def vote(request, question_id):
    return HttpResponse('Your are looking at vote Question %s.'%question_id)
====|
python manage.py runserver
瀏覽器:http://127.0.0.1:8000/polls
新建polls/templates/polls/detail.html
====
<h1>{{ question.question_text }}<h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
====|
編輯polls/views.py
====
from django.shortcuts import render
from .models import Question
from django.http import HttpResponse, Http404


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

def detail(request, question_id):
    try:
        question = Qustion.objects.get(pk=question_id)
    except:
        raise Http404('The question not exists.')
    return render(request, 'polls/detail.html', {'question': question})

def results(request, question_id):
    return HttpResponse('Your are looking at results Question %s.'%question_id)

def vote(request, question_id):
    return HttpResponse('Your are looking at vote Question %s.'%question_id)
====|
python manage.py runserver
瀏覽器:http://127.0.0.1:8000/polls/1
編輯polls/views.py
====
from django.shortcuts import get_object_or_404, render
from .models import Question


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

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

def results(request, question_id):
    return HttpResponse('Your are looking at results Question %s.'%question_id)

def vote(request, question_id):
    return HttpResponse('Your are looking at vote Question %s.'%question_id)
====|
python manage.py runserver
瀏覽器:http://127.0.0.1:8000/polls/1
編輯polls/templates/polls/index.html
====
{% if last_question_list %}
    <ul>
        {% for question in last_question_list %}
            <li><a href='{% url 'detail' question.id %}'>{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>no polls.</p>
{% endif %}
====|
python manage.py runserver
瀏覽器:http://127.0.0.1:8000/polls/1
編輯polls/urls.py
====
from django.urls import path
from . import views

app_name = 'polls'
urlpatterns = [
    path('', views.index, name='index'),
    path('<int:question_id>/', views.detail, name='detail'),
    path('<int:question_id>/results/', views.results, name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]
====|
編輯polls/templates/polls/index.html
====
{% if last_question_list %}
    <ul>
     {% for question in last_question_list %}
         <li><a href='{% url 'polls:detail' question.id %}'>{{ question.quetion_text }}</a></li>
     {% endfor %}
    </ul>
{% else %}
    <p>no polls.</p>
{% endif %}
====|
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末霞揉,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子夯膀,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件生宛,死亡現(xiàn)場離奇詭異寞酿,居然都是意外死亡趟妥,警方通過查閱死者的電腦和手機(jī)祟敛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進(jìn)店門倍奢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人垒棋,你說我怎么就攤上這事』驹祝” “怎么了叼架?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵畔裕,是天一觀的道長。 經(jīng)常有香客問我乖订,道長扮饶,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任乍构,我火速辦了婚禮甜无,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘哥遮。我一直安慰自己岂丘,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布眠饮。 她就那樣靜靜地躺著奥帘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪仪召。 梳的紋絲不亂的頭發(fā)上寨蹋,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天,我揣著相機(jī)與錄音扔茅,去河邊找鬼已旧。 笑死,一個胖子當(dāng)著我的面吹牛召娜,可吹牛的內(nèi)容都是我干的运褪。 我是一名探鬼主播,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼萤晴,長吁一口氣:“原來是場噩夢啊……” “哼吐句!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起店读,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤嗦枢,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后屯断,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體文虏,經(jīng)...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年殖演,在試婚紗的時候發(fā)現(xiàn)自己被綠了氧秘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡趴久,死狀恐怖丸相,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情彼棍,我是刑警寧澤灭忠,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布膳算,位于F島的核電站,受9級特大地震影響弛作,放射性物質(zhì)發(fā)生泄漏涕蜂。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一映琳、第九天 我趴在偏房一處隱蔽的房頂上張望机隙。 院中可真熱鬧,春花似錦萨西、人聲如沸有鹿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽印颤。三九已至,卻和暖如春穿肄,著一層夾襖步出監(jiān)牢的瞬間年局,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工咸产, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留矢否,地道東北人。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓脑溢,卻偏偏與公主長得像僵朗,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子屑彻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內(nèi)容