Django
Posted 起航追梦人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django相关的知识,希望对你有一定的参考价值。
1、创建新的虚拟环境
virtualenv lcv_search
2、在虚拟环境中用豆瓣源安装Django
pip install https://pypi.douban.com/simple/ django
3、用PyCharm创新Django项目
4、创建好后目录如下
5、运行Lcs_search项目,进入:http://127.0.0.1:8000/ 显示如下
6、在Lcs_search文件夹的urls.py文件中配置首页url
from django.conf.urls import url from django.contrib import admin from django.views.generic import TemplateView urlpatterns = [ url(r\'^admin/\', admin.site.urls), url(r\'^$\', TemplateView.as_view(template_name="index.html"), name="index"), #index.html为前端文件,name自定义取 ]
7、在Lcs_search文件夹的settings.py文件中倒入样式文件夹
#在settings.py文件最后添加 STATICFILES_DIRS = [ os.path.join(BASE_DIR, \'Front\') #BASE_DIR是原settings.py文件中的路径变量os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ]
8、修改.html文件中的css和js引用的路径
{% load staticfiles%} #Django固定写法 <link href="{% static \'css/style.css\'%}" rel="stylesheet" type="text/css" /> <link href="{% static \'css/index.css\'%}" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="{% static \'js/jquery.js\'%}"></script> <script type="text/javascript" src="{% static \'js/global.js\'%}"></script>
#{% static \'js/jquery.js\'%}中的static为settings.py文件的变量:STATIC_URL = \'/static/\' ,
{% static \'js/jquery.js\'%}也可以这样写\'/static/js/jquery.js\'
9、修改完成后显示正确了
10、在urls.py中添加搜索提示连接
from django.conf.urls import url from django.contrib import admin from django.views.generic import TemplateView from search.views import SearchSuggest urlpatterns = [ url(r\'^admin/\', admin.site.urls), url(r\'^$\', TemplateView.as_view(template_name="index.html"), name="index"), url(r\'^suggest/$\', SearchSuggest.as_view(), name="suggest"),#自动补全链接接口 ]
11、在Lsv_search下的search的models.py文件中加入代码(在虚拟环境内安装elasticsearch-dsl)
from elasticsearch_dsl import DocType, Date, Nested, Boolean, \\ analyzer, InnerObjectWrapper, Completion, Keyword, Text, Integer from elasticsearch_dsl.analysis import CustomAnalyzer as _CustomAnalyzer from elasticsearch_dsl.connections import connections connections.create_connection(hosts=["localhost"]) class CustomAnalyzer(_CustomAnalyzer): def get_analysis_definition(self): return {} ik_analyzer = CustomAnalyzer("ik_max_word", filter=["lowercase"]) class ArticleType(DocType): #伯乐在线文章类型 suggest = Completion(analyzer=ik_analyzer) title = Text(analyzer="ik_max_word") create_date = Date() url = Keyword() url_object_id = Keyword() front_image_url = Keyword() front_image_path = Keyword() praise_nums = Integer() comment_nums = Integer() fav_nums = Integer() tags = Text(analyzer="ik_max_word") content = Text(analyzer="ik_max_word") class Meta: index = "jobbole" doc_type = "article" if __name__ == "__main__": ArticleType.init()
12、在Lsv_search下的search的views.py文件中加入代码
from django.shortcuts import render from django.views.generic.base import View from search.models import ArticleType from django.http import HttpResponse import json # Create your views here. class SearchSuggest(View): def get(self, request): key_words = request.GET.get(\'s\', \'\') re_datas = [] if key_words: s = ArticleType.search() s = s.suggest(\'my_suggest\', key_words, completion={ \'field\': \'suggest\', \'fuzzy\': {\'fuzziness\': 2}, \'size\': 10 }) suggestion = s.execute_suggest() for match in suggestion.my_suggest[0].options: source = match._source re_datas.append(source[\'title\']) return HttpResponse(json.dumps(re_datas), content_type=\'application/json\')
10-12完成搜索提示(自动补全功能),效果如下
13、在urls.py中添加搜索功能接口
以上是关于Django的主要内容,如果未能解决你的问题,请参考以下文章
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段