python django

alexmaodali發表於2024-04-05

Django 3.2

1.create first site

django-admin startproject mysite
cd mysite
python manage.py runserver

2.First view

view.py:

from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.
def index(request):
    return HttpResponse("Hello.There is the polls index 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),
]

localhost/polls/

3.啟用 modle

settings:
    "polls.apps.PollsConfig",
from django.db import models


# Create your models here.
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField("date published")


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

python manage.py migrate
python manage.py makemigrations polls
#$ python manage.py sqlmigrate polls 0001
python manage.py migrate

編輯 models.py 檔案,改變模型。

執行 python manage.py makemigrations 為模型的改變生成遷移檔案。

執行 python manage.py migrate 來應用資料庫遷移。

4.API

$ python manage.py shell
>>> Question.objects.filter(question_text__startswith='What')

5.管理介面 admin

$ python manage.py createsuperuser
root root@root.com rootfrom django.contrib import admin

# Register your models here.
from .models import Question

admin.site.register(Question)

6.url

from django.urls import path
from . import views

urlpatterns = [
    # /polls/
    path("", views.index, name="index"),
    # /polls/33/
    path("<int:qustion_id>", views.detail, name="detail"),
    # /polls/33/results/
    path("<int:qustion_id>/results/", views.results, name="results"),
    # /polls/33/vote/
    path("<int:qustion_id>/vote/", views.vote, name="vote"),
]
from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.
def index(request):
    return HttpResponse("Hello.There is the polls index page!")


def detail(request, qustion_id):
    response = "You're looking at question %s."
    return HttpResponse(response % qustion_id)
    return HttpResponse(response % qustion_id)


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


def vote(request, qustion_id):
    response = "You're looking at question %s."
    return HttpResponse(response % qustion_id)

7.檢視,path

    <ul>
      {% for question in latest_question_list %}
      <li>
        <!-- /polls/33/ -->
        <!-- path("<int:qustion_id>/", views.detail, name="detail"), -->
        <!--  -->
        <a href="{%url 'polls:detail' question.id%}">{{ question.question_text }}</a>
      </li>
      {% endfor %}
    </ul>
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)


def detail(request, qustion_id):
    # http://127.0.0.1:8000/polls/1
    question = get_object_or_404(Question, pk=qustion_id)
    dic = {"question": question}
    return render(request, "polls/detail.html", dic)

相關文章