net19880504

本讲会对种子搜索网站的开发过程进行详细的讲解。

 

源码地址:https://github.com/geeeeeeeek/bt

项目开发过程

项目简介

该项目是基于python的web类库django开发的一套web网站,做为本人的毕业设计。
本人的研究方向是一项关于搜索的研究项目。在该项目中,笔者开发了一个简单版的搜索网站,实现了对数据库数据的检索和更新。 网站域名为bt.mypython.me

启动项目

django-admin startproject bt

创建应用

python3 manage.py startapp app

model设计

主要是对提交的链接进行设计,在此项目中,我们需要展示链接的名称、url、联系人、链接简介等字段。

设计字段如下:

class Link(models.Model):
list_display = ("url","desc","contact")
url = models.CharField(max_length=100,blank=True, null=True)
title = models.CharField(max_length=100,blank=True, null=True)
size = models.CharField(max_length=100,blank=True, null=True)
hot = models.IntegerField(default=0)
desc = models.CharField(max_length=200,blank=True, null=True)
contact = models.CharField(max_length=100,blank=True, null=True)
status = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True, null=True)
objects = LinkQuerySet.as_manager()

 

业务编写

本项目一共分为4个页面,分别是首页、搜索列表页、详情页、链接提交页。

我们一一讲解

首页

首先是首页,它的模版位于templates/app/index.html 它主要是用来展示首页内容, 并提交搜索词,到搜索接口,所有的接口都位于app/urls.py里面,如下

app_name = \'app\'
urlpatterns = [
path(\'index\', views.IndexView.as_view(), name=\'index\'),
path(\'search\', views.SearchView.as_view(), name=\'search\'),
path(\'detail/<int:pk>\', views.DetailView.as_view(), name=\'detail\'),
path(\'commit\', views.CommitView.as_view(), name=\'commit\'),
]

 

我们设置首页的路由为IndexView, 开始编写IndexView的代码。它的代码非常简单:

class IndexView(generic.TemplateView):
template_name = \'app/index.html\'

 

仅仅是展示了首页页面,首页将搜索词交给了search来处理,这一点,我们从index.html关于form的代码中可以看到, 提交给了url ‘app:search’

<form id="search-form" action="{% url \'app:search\' %}" enctype="multipart/form-data" method="get" role="form">
<input type="text" id="search" name="q" autocomplete="off" placeholder="搜搜你懂的">
<input type="submit" id="btnSearch" value="搜 索" class="blue">
</form>

 

列表展示页

从urls.py中可知,app:search指向了SearchView,这个类是本项目的核心代码,它实现了搜索的全过程。

class SearchView(generic.ListView):
model = Link
template_name = \'app/search.html\'
context_object_name = \'link_list\'
paginate_by = 10
q = \'\' # 搜索词
duration = 0 # 耗时
record_count = 0

def get_context_data(self, *, object_list=None, **kwargs):
context = super(SearchView, self).get_context_data(**kwargs)
paginator = context.get(\'paginator\')
page = context.get(\'page_obj\')
page_list = get_page_list(paginator, page)
context[\'page_list\'] = page_list
context[\'q\'] = self.q
context[\'duration\'] = round(self.duration,6)
context[\'record_count\'] = self.record_count
return context

def get_queryset(self):
start = time.time()
self.q = self.request.GET.get("q", "")
search_list = Link.objects.get_search_list(self.q)
# 如搜索为空,则放假数据
if len(search_list) <= 0:
search_list = Link.objects.get_fake_list()
end = time.time()
self.duration = end - start
self.record_count = len(search_list)
return search_list

继承了ListView通用类,通过get_queryset()回调函数来实现搜索功能,并通过get_context_data来传递额外的数据给前端。即是列表展示页。

详情页

我们再来开发详情页,从urls.py中看到,详情页是由DetailView来实现的,我们来窥探它的全貌:

class DetailView(generic.DetailView):
model = Link
template_name = \'app/detail.html\'

def get_object(self, queryset=None):
obj = super().get_object()
obj.increase_hot_count()
return obj

def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
recommend_list = Link.objects.get_recommend_list()
context[\'recommend_list\'] = recommend_list
return context

 

它很简单,继承了DetailView通用模板类来显示详情。

链接提交页

最后再来看一下链接提交页,它是由CommitView来实现的。同样是观看代码:

class CommitView(generic.CreateView):

model = Link
form_class = CommitForm
template_name = \'app/commit.html\'

@ratelimit(key=\'ip\', rate=\'2/m\')
def post(self, request, *args, **kwargs):
was_limited = getattr(request, \'limited\', False)
if was_limited:
messages.warning(self.request, "操作太频繁了,请1分钟后再试")
return render(request, \'app/commit.html\', {\'form\': CommitForm()})
return super().post(request, *args, **kwargs)

def get_success_url(self):
messages.success(self.request, "提交成功! 审核期3个工作日。")
return reverse(\'app:commit\')

 

它是继承自CreateView,因为是创建操作嘛,在post中,我们通过ratelimit来限制提交次数。

运行项目

python3 manage.py runserver

分类:

技术点:

相关文章:

  • 2021-11-27
  • 2021-10-16
  • 2021-10-01
  • 2021-12-29
  • 2021-08-04
  • 2021-11-29
  • 2021-09-20
猜你喜欢
  • 2021-11-07
  • 2021-11-29
  • 2021-12-09
  • 2021-12-10
相关资源
相似解决方案