环境:Python3.11
Django5.0.1
python -m venv ll_env
ll_env\Scripts\activate
控制台前方出现高亮即为成功
pip install Django
python manage.py migrate
python manage.py startapp learning_logs
打开learning_logs/models.py建立两个模型
from django.db import models
# Create your models here.
class Topic(models.Model):
# """用户学习的主题"""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
# """返回模型的字符串表示"""
return self.text
class Entry(models.Model):
"""学到的有关某个主题的具体知识"""
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
"""返回模型的字符串表示"""
return self.text[:50] + "..."
打开settings.py
# 我的应用程序
'learning_logs',
python manage.py makemigrations learning_logs
python manage.py migrate
在控制台输入:
python manage.py createsuperuser
根据提示输入信息
from django.contrib import admin
# Register your models here.
from learning_logs.models import Topic
admin.site.register(Topic)
from learning_logs.models import Entry
admin.site.register(Entry)
书中提供的信息如下:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('learning_logs.urls', namespace='learning_logs')),
# url(r'', include('learning_logs.urls', namespace='learning_logs')), # Django2.0使用djang.urls.path
]
"""定义learning_logs的URL模式"""
from django.urls import path
#从当前的urls.py模块所在的文件夹中导入视图
from . import views
#变量urlpatterns是一个列表,包含可在应用程序learning_logs中请求的网页
app_name = 'learning_logs'
urlpatterns = [
#主页
#第一个是正则表达式,r让Python将接下来的字符串视为原始字符串,引号正则表达式始于和终于何处
#第二个实参指定了要调用的视图函数
#第三个实参将这个URL模式的名称指定为index
path(r'',views.index,name='index'),
# 显示所有的主题
path(r'topics/', views.topics, name='topics'),
# 特定主题的详细页面
path(r'^topics/(?P\d+)/$', views.topic, name='topic'),
]
from django.shortcuts import render
from .models import Topic
# Create your views here.
def index(request):
"""学习笔记的主页"""
return render(request, 'learning_logs/index.html')
def topics(request):
"""显示所有的主题"""
topics = Topic.objects.order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
def topic(request, topic_id):
"""显示单个主题及其所有的条目"""
topic = Topic.objects.get(id=topic_id)
entries = topic.entry_set.order_by('-date_added')
context = {'topic': topic, 'entries': entries}
return render(request, 'learning_logs/topic.html', context)
按顺序新建文件夹以及四个页面
Title
{% block content %}{% endblock content %}
index.html
Title
{% extends "learning_logs/base.html" %}
{% block content %}
Learning Log helps you keep track of your learning, for any topic you're
learning about.
{% endblock content %}
topic.html
Title
{% extends "learning_logs/base.html" %}
{% block content %}
Topic: {{ topic }}
Entries:
{% for entry in entries %}
-
{{ entry.date_added|date:'M d, Y H:i' }}
{{ entry.text|linebreaks }}
{% empty %}
-
There are no entries for this topic yet.
{% endfor %}
{% endblock content %}
topics.html
Topics
{% extends "learning_logs/base.html" %}
{% block content %}
Topics
{% for topic in topics %}
-
{{ topic }}
{% empty %}
- No topics have been added yet.
{% endfor %}
{% endblock content %}