欢迎来我的个人博客:fizzyi
项目介绍
缘由:前几天爬取了姓名大全网站,数据在140万左右,所以想做一个django网站来展示所有的数据,顺便复习下django的知识以及上线的步骤。
前端模版:来自于 凡科网
环境:python 3.6 Django 1.11
gtihub地址:https://github.com/Fizzyi/baijiaxing/tree/master/baijiaxing
线上地址:http://baijiaxing.fizzyi.com/
代码
1准备工作
新建django项目
django-admin startproject baijiaxing
新建django应用
python manage.py startapp app
修改setting.py中的配置文件
DEBUG = True
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
...
'app.apps.AppConfig',
]
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')]
}
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test1',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': 3306
}
}
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
配置baijiaxing/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^app/',include('app.urls',namespace='app')),
]
导入前端模版和静态资源
2 建立数据库模型
app/models.py
from django.db import models
# Create your models here.
class Baijiaxing(models.Model):
id = models.IntegerField(primary_key=True)
xingshi = models.CharField(max_length=256)
href = models.CharField(max_length=1024)
xingshi_zhongwen = models.CharField(max_length=256)
class Meta:
db_table = "baijiaxing"
class Xingming(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=256)
the_same_people_number = models.CharField(max_length=256)
boy_ratio = models.CharField(max_length=256)
girl_ratio = models.CharField(max_length=256)
five_elements = models.CharField(max_length=256)
three_talents = models.CharField(max_length=256)
xingshi = models.ForeignKey(Baijiaxing)
class Meta:
db_table = 'xingming'
然后在终端执行
python3 manage.py makemigrations
因为我数据库本来是已经建好了,姓氏和姓名的数据都已经存在数据库中,所以就不需要执行 python manage.py migrate
3 获取所有的姓氏
app/urls.py
urlpatterns = [
url(r'^index/',app_views.get_all,name='index'),
]
app/views.py
#获取所有的姓氏
def get_all(request):
xingshi_lists = Baijiaxing.objects.all()
the_max_leng = len(xingshi_lists)
the_xingshi_list = []
for i in range(0,len(list(xingshi_lists)),6):
the_list = []
for m in range(i,i+6):
if m < the_max_leng:
the_list.append(xingshi_lists[m])
the_xingshi_list.append(the_list)
return render(request,'index.html',{'the_xingshi_list':the_xingshi_list})
4 获取每个姓氏的所有姓名并且进行分页
app/urls.py
from app import views as app_views
urlpatterns = [
url(r'^get_one_xingshi/(?P<xingshi_id>\d+)',app_views.get_one_xingshi,name='get_one_xingshi'),
]
app/views.py
#获取每个姓氏的所有姓名并且进行分页
def get_one_xingshi(request,xingshi_id):
try:
xingshi = Baijiaxing.objects.filter(id=xingshi_id)[0]
content = Xingming.objects.filter(xingshi_id = int(xingshi_id)).order_by('id')
page_num = int(request.GET.get('page',1))
paginator = Paginator(content,70)
page = paginator.page(page_num)
if not content:
msg = '暂无此姓氏名字,快点击右上角添加吧'
else:
msg = ''
return render(request,'components.html',{'page':page,'xingshi':xingshi,'msg':msg})
except:
return HttpResponse('暂无数据')
5 获取每个姓名的信息
app/urls.py
from app import views as app_views
urlpatterns = [
url(r'^get_one_info/(?P<xingming_id>\d+)',app_views.get_one_info,name='get_one_info'),
]
app/views.py
#获取每个姓名的信息
def get_one_info(request,xingming_id):
try:
the_info = Xingming.objects.filter(id = int(xingming_id))[0]
return render(request,'tutorial.html',{'the_info':the_info})
except:
return HttpResponse('暂无数据')
6 添加姓名
App/urls.py
from app import views as app_views
urlpatterns = [
url(r'^get_one_info/(?P<xingming_id>\d+)',app_views.get_one_info,name='get_one_info'),
]
app/views.py
#添加姓名
def add_one_xingming(request):
if request.method == 'GET':
return render(request,'landing.html')
if request.method == 'POST':
#获取传回来的数据
xingshi = request.POST.get('xingshi')
xingming = request.POST.get('xingming')
five_elements = request.POST.get('five_elements')
three_talents = request.POST.get('three_talents')
#判断是否是真的没有这个姓名
the_name = Xingming.objects.filter(name=xingming)
if not the_name:
#查询数据库中是否有该形式,有 只在姓名表上添加,没有 要在百家姓和姓名表都添加
if not Baijiaxing.objects.filter(xingshi_zhongwen=xingshi):
#没有这个姓氏
#将姓氏转换为pinyin+数字 数字代表拼音
the_xingshi = pinyin.get(xingshi, format='numerical', delimiter=" ")
Baijiaxing.objects.create(xingshi=the_xingshi,xingshi_zhongwen=xingshi)
the_xingshi_id = Baijiaxing.objects.filter(xingshi_zhongwen=xingshi).first().id
Xingming.objects.create(name=xingming,five_elements=five_elements,three_talents=three_talents,xingshi_id=the_xingshi_id)
return HttpResponseRedirect(reverse('app:index'))
else:
the_xingming = the_name.first()
msg = '该姓名已经存在'
return render(request,'landing.html',{'the_xingming':the_xingming,'msg':msg})
添加姓名用了一个第三方包pinyin,将中文转换成拼音加数字 数字为1234,代表声调