毕设-基于 Python 的种子搜索网站开发

2019-03-11 10:59:06 +08:00
 lovezww2011

网站演示: https://bt.mypython.me

源码地址: 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
3222 次点击
所在节点    程序员
19 条回复
zbl430
2019-03-11 11:13:23 +08:00
建议用 es 做搜索
kios
2019-03-11 11:21:14 +08:00
以前用 twisted 写过一个 DHT network 的爬虫,看标题以为和我写的东西差不多 原来只是个展示的壳子...
meiyoumingzi6
2019-03-11 13:55:23 +08:00
Page not found (404)
Request Method: GET
Request URL: https://bt.mypython.me/23333
Using the URLconf defined in bt.urls, Django tried these URL patterns, in this order:

admin/
app/
[name='home']
The current path, 23333, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.





所以 debug 模式也不关掉么
ByteChen
2019-03-11 14:43:05 +08:00
搜索不够准确哦 尤其在没有符合的目标的时候 没有反馈
huangdayu
2019-03-11 14:47:47 +08:00
搜索似乎不太理想,排序似乎无效?
cc3630
2019-03-11 14:48:31 +08:00
说实话,这个毕设有点简单了,几天就能完成。。。
lovezww2011
2019-03-11 14:52:28 +08:00
@huangdayu 因为库里的数据太少了,所以当没有搜索到的时候,只抛给用户一些假数据。 首页搜索栏下面的那几个是可以搜到的。^_^
chuanwu
2019-03-11 15:13:30 +08:00
lovezww2011
2019-03-11 15:36:06 +08:00
@chuanwu 请不要泄漏密码。^-^
chuanwu
2019-03-11 17:20:03 +08:00
@lovezww2011 截图里我打马赛克了 relax
yuzhiquan
2019-03-11 17:42:21 +08:00
话说 django admin 不是这个样子啊
Trim21
2019-03-11 17:47:36 +08:00
点进来之前以为是 dht 搜索的那种…
rust
2019-03-11 23:25:34 +08:00
现在的大学生就这么弱?
enrolls
2019-03-12 00:00:01 +08:00
直接给你成品,YUhSMGNEb3ZMM052ZFhOdmRXTnBiR2t1WTI5dEx3PT0= ,上次搜 TVB 缺失电视剧的时候发现的。
lovezww2011
2019-03-12 09:11:05 +08:00
@enrolls 成品?什么成品?片子吗
lingo
2019-03-12 09:35:29 +08:00
@enrolls 两层,讲究。不过这站好清爽。
polebug
2019-03-12 09:36:54 +08:00
....这毕设也太水了 两天就能搞完...
Sephiro
2019-03-12 10:19:32 +08:00
是不是太划了 有些简单过头了
lovezww2011
2019-03-12 15:07:26 +08:00
@polebug 专科学校的毕业设计,都很水。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/543232

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX