djangoの2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了djangoの2相关的知识,希望对你有一定的参考价值。

谷鸽或百度的镜像:
 
1?E:\django下建个文件夹名为搜索引擎→PyCharm新建项目选Django→location改为E:\django\搜索引擎→More Settings的Application写个search
***************分割线***************
2?项目文件夹搜索引擎:
 
①settings.py,只改语言和时区
 
②urls.py:
 
from django.conf.urls import url
from django.contrib import admin
from search import views
 
urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    url(r‘^$‘, views.index),
    # url(r‘^s‘, views.so),  #搜索网址尾是?及各参数,不加/和$;百度是s,谷鸽是search
    url(r‘^search‘, views.so),
]
***************分割线***************
3?应用文件夹search:
 
①新建俩文件夹templates和static:templates里存放复制自谷鸽首页的googleIndex.html,和强子院子写的baiduIndex.html;static放images\so.gif等强子所做的百度前端样式。
 
②views.py:
 
from django.shortcuts import render
from django.http import HttpResponse
import requests
from fake_useragent import UserAgent
 
def copySearchEngineData(wd):
    h = {‘User-Agent‘: UserAgent().random}
    # html=requests.get(‘https://www.baidu.com%s‘ %wd,headers=h).text
    #static文件夹,若是和manage.py同级,则前无/;若用的是在某个应用内建的,则加/
    # html=html.replace(‘//www.baidu.com/img/baidu_jgylogo3.gif‘,‘/static/images/so.gif‘)
    s = requests.session()
    javascript=s.get(‘https://xs5.rqiao.net‘, headers=h).text.split(‘cookie\‘ : "‘)[1].split(‘"‘)[0]
    s.cookies[‘verynginx_sign_javascript‘]=javascript
    html = s.get(‘https://xs5.rqiao.net%s‘ %wd, headers=h).text
    oldLogo=‘/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png‘
    html=html.replace(oldLogo,‘/static/images/so.gif‘)
    return html
 
def index(request):
    return render(request,‘googleIndex.html‘)   #baiduIndex.html
 
def so(request):
    # word=request.GET[‘wd‘]    #取输入框的值:text框的name属性的值,B是wd,Google是q
    word=request.get_full_path()  #取网址中不含域名的后半截
    result=copySearchEngineData(word)
    return HttpResponse(result) #展示的是html文件用render,网页源代码则用HttpResponse
 
baiduIndex.html:
 

{% load static %}

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="Keywords" content="关键词,关键字">
<meta name="Description" content="描述信息">
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<title>百度镜像</title>

<!--css 样式 化妆 整容 美图-->
<style type="text/css">
*{margin:0;}
html,body{height:100%;}
body{background:url("{% static ‘/img/1.jpg‘ %}")center 0;background-size:100% 100%;}
/*S box*/
.box{width:640px;height:150px;margin:50px auto 0;}
/*logo*/
.box .logo{width:210px;height:100px;margin:0 auto 20px;background:url("{% static ‘/img/1.png‘ %}")center 0;background-size:100% 100%;}
/*搜索框*/
.box .search{width:640px;height:40px;}
.box .search .txt{width:540px;height:38px;border:none;outline:none;font-size:18px;font-family:"方正启体简体";color:#888;text-indent:10px;float:left;}
.box .search .btn{width:100px;height:40px;float:right;border:none;outline:none;background:url("{% static ‘/img/2.png‘ %}");
background-position:-206px 0;font-size:18px;font-family:"微软雅黑";}
.box .search .btn:active{box-shadow:0 0 5px #000 inset;font-size:16px;}
/*候选框*/
.box .search .show{display:none;width:540px;background:#fff;float:left;margin:2px 0;box-shadow:0 0 5px #fff;}
.box .search .show div{font-size:16px;font-family:"微软雅黑";text-indent:10px;line-height:28px;}
.box .search .show div:hover{background:rgba(0,0,0,0.2);}
/*E box*/
</style>

</head>

<body>
<p style="text-align:center;">
<span style="color:red;font-size:32px;"><strong><span style="color:#FFFFFF;">更纯净,无广告!</span><span style="font-size:18px;color:#ADFF2F;">绿色的网络搜索环境</span></strong></span>
</p>

<!--S box-->
<div class="box">
<div class="logo"></div>
<div class="search">
<form action="/s" method="get">
<input type="text" class="txt" name="wd" id="txt"/>
<input type="submit" class="btn" value="百度一下">
</form>
</div>
</div>

<!--E box-->
<br>
<br>
<script src="{% static ‘/js/jquery-1.11.1.min.js‘ %}"></script>

<script>
$(function(){
$(".show").on("click","div",function(){
var text = $(this).text();
$(".txt").val(text);//给文本框赋值
$(".show").fadeOut("solw",function(){
$(this).hide();
//跳转
window.location.href = "/s?wd="+text+"";
});
});
});

//当键盘按下抬起来的时候
$(".txt").keyup(function(e){
var value = $(this).val();
//显示
$(".show").empty().show();
if(!value)return;
var txt = $("#txt").val();
$.ajax({
url: "/key?wd=" + txt,
type: "get",
success: function (data) {
var arr = eval(data);
var html = "";
for(var i = 0; i < arr.length; i++){
html += "<div>"+arr[i]+"</div>";}
$(".show").html(html);},
});
});
</script>

</body>
</html>

以上是关于djangoの2的主要内容,如果未能解决你的问题,请参考以下文章

Django 2021年最新版教程2Django项目目录结构中各文件的作用

60 django- cookie和session 以及中间件

djangoの2

pythonのdjango初体验

Django基础

Django框架