myAPP
{{myText}}
# python变量的写法,{{python variety}}
search.html
MyProject
{{result}}
def search(request):
text = request.POST.get("myText")
answer = {"result":text}
return render(request, 'search.html', answer)
from django.conf.urls import include,url
from . import views
urlpatterns = [
url(r'index/', views.index),
url(r'search/', views.search), # 新增
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myDB',
'USER': '', # 用户名
'PASSWORD': '', # 密码
'HOST': 'localhost',
'PORT': 3306, # mysql默认端口是3306,如果你改了mysql的端口配置,请修改
}
}
from django.db import models
# Create your models here.
class myTable(models.Model):
index = models.AutoField(primary_key=True) # 自增列,在Django中不写也可自动生成
word = models.CharField(max_length=32)
comment = models.CharField(max_length=32, null=True)
from django.shortcuts import render
from myApp.models import myTable # 新增
# Create your views here
def index(request):
return render(request, 'index.html')
def search(request):
text = request.POST.get("myText")
seg = text.split(":") # 新增
myTable.objects.create(word=seg[0], comment=seg[1]) # 新增
return render(request, 'index.html') # 修改
from django.conf.urls import include,url
from . import views
urlpatterns = [
url(r'index/', views.index),
url(r'search/', views.search),
url(r'totalWords/(\d*)', views.totalWords), # 新增
]
from django.core import paginator
def totalWords(request, index):
# 获取所有的的数据,list是一个列表,包含有所有数据对应的实例对象
allWords = myTable.objects.all()
# 使用Paginator方法返回一个分页的对象
# 这个对象包括所有数据,分页的情况
pag = paginator.Paginator(allWords, 10) # param:所有数据,每页的数据量
# 使用此判断语句是为了在用户在刚跳转时也能访问第一页
if index == '':
index = 1
# 返回指定(index)页的数据,用于呈现在指定页上
page = pag.page(index)
# 构造上下文,以便html文件中能调用对应页的数据
context = {
'page': page,
}
return render(request, 'totalWords.html', context)
LUWR
word
comment
{% for each in page %}
{{ each.word }}
{{ each.comment }}
{% endfor %}
{% for index in page.paginator.page_range %}
{% if index == page.number %}
{{ index }}
{% else %}
{{ index }}
{% endif %}
{% endfor %}