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         - 管理员站点
    django.contrib.auth      - 认证授权系统
    django.contrib.contenttypes  - 内容类型框架
    django.contrib.sessions      - 会话框架
    django.contrib.messages      - 消息框架
    django.contrib.staticfiles   - 管理静态文件的框架
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('/', views.detail, name='detail'),
    path('/results', views.results, name='results'),
    path('/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 %}
    
{% else %}
    

No polls.

{% 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 ====

{{ question.question_text }}

    {% for choice in question.choice_set.all %}
  • {{ choice.choice_text }}
  • {% endfor %}
====| 编辑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 %} {% else %}

no polls.

{% 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('/', views.detail, name='detail'), path('/results/', views.results, name='results'), path('/vote/', views.vote, name='vote'), ] ====| 编辑polls/templates/polls/index.html ==== {% if last_question_list %} {% else %}

no polls.

{% endif %} ====|

你可能感兴趣的:(django文档)