django 加载html与form 前后台交互

一、加载html

1.编辑scrapyweb/urls.py

from django.conf.urls import url
from webapp.views import index
urlpatterns = [
    url(r'^$', index),
]

2.编辑scrapyweb/webapp/views.py


from django.shortcuts import render_to_response

def index(req):
     return render_to_response('index.html')

3.编辑scrapyweb/webapp/templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>input</title>
    <style >
    .s_url{width:523px;height:32px;line-height:22px;margin:5px 0 0 7px;padding:0;-webkit-appearance:none}
    .s_sb{width:95px;height:36px;padding:0;background-color:#ddd;background-position:0 -48px;border:0;cursor:pointer}
    </style>
</head>
<body>
<form >
    <span >
        <input id="kw" name="wd" class="s_url" value="输入url" maxlength="255" autocomplete="off">
    </span>
    <span >
        <input type="submit" id="su" value="爬取" class="s_sb">
    </span>
</form>
</body>
</html>


4.页面展示


二、form前后台交互--页面跳转

一、编辑scrapyweb/urls.py


from django.conf.urls import url
from webapp.views import index ,scrapy
urlpatterns = [
    url(r'^$', index),
    url(r'^scrapy$', scrapy),
]

2.编辑scrapyweb/webapp/views.py

from django.http import HttpResponse
from django.shortcuts import render_to_response

def index(req):
     return render_to_response('index.html')

# 接收请求数据
def scrapy(request):
    request.encoding='utf-8'
    message='你搜索的内容为: '+request.GET['wd']
    return HttpResponse(message)

3.编辑scrapyweb/webapp/templates/index.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>input</title>
    <style >
    .s_url{width:523px;height:32px;line-height:22px;margin:5px 0 0 7px;padding:0;-webkit-appearance:none}
    .s_sb{width:95px;height:36px;padding:0;background-color:#ddd;background-position:0 -48px;border:0;cursor:pointer}
    </style>
</head>
<body>
<form action="scrapy" method="get"> //注意这里的设置
    <span >
        <input id="kw" name="wd" class="s_url" value="输入url" maxlength="255" autocomplete="off">
    </span>
    <span >
        <input type="submit" id="su" value="爬取" class="s_sb">
    </span>
</form>
</body>
</html>

4.页面展示

二、form前后台交互--页面不跳转

一、编辑scrapyweb/urls.py

from django.conf.urls import url
from webapp.views import index ,do_scrapy_url
urlpatterns = [
    url(r'^$', index),
    url(r'^do_scrapy_url$', do_scrapy_url),
]

2.编辑scrapyweb/webapp/views.py

from django.shortcuts import render
# Create your views here.
from django.shortcuts import render_to_response
from webapp.scrapyall import start_crawler


def index(req):
    #return HttpResponse('hello django')
    # url=start_crawler("https://blog.csdn.net/")
     return render_to_response('index.html')

# 接收请求数据
def do_scrapy_url(request):
    #u = request.POST.get('user', None)
    #url = request.POST.get('urlval', None)
    ctx = {}
    if request.GET:
        ctx['url'] = request.GET['urlval']
    else:
        ctx['url']='没有url'
    return render(request,'index.html', ctx )

 

3.编辑scrapyweb/webapp/templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>scrapy</title>
    <style >
    .s_url{width:523px;height:32px;line-height:22px;margin:5px 0 0 7px;padding:0;-webkit-appearance:none}
    .s_sb{width:95px;height:36px;padding:0;background-color:#ddd;background-position:0 -48px;border:0;cursor:pointer}
    </style>
</head>
<body >
<form  action="do_scrapy_url" method="get">
    {% csrf_token %}
    <input name="urlval" class="s_url" placeholder="请输入url" maxlength="255" autocomplete="off">
    <input type="submit"  value="爬取" class="s_sb" >
</form>

<p>{{ url }}</p>

</body>
</html>

4.页面展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值