Python Django框架 2019(04)

GET方法。视图显示和请求处理分成两个函数处理。

模板form.html

<form action=”/west/investigate/” method=”get”>
  <input type=”text” name=”staff_in”>
  <input type=”submit” value=”Submit”>
</form>

在west/views.py中定义一个视图form()来显示表格:

def form(request):
    return render(request, ‘form.html’)

设置urls.py,让对[site]/west/form/的访问,指向该视图。

url(r’^form/’,views.form),

在west/views.py中定义investigate()来处理该表格提交的数据:

def investigate(request):
    rlt = request.GET[‘staff_in’]
    return HttpResponse(“input:   ” + rlt)

设置urls.py,让该处理函数对应action的URL([site]/west/investigate/)。

url(r’^investigate/’,views.investigate),

访问http://127.0.0.1:8000/west/form时,将显示:

提交表格后,页面/west/investigate。investigate()读取字符串后,在页面上显示出来。

OK~~~~~~~~~~~

提交数据时更常用POST方法。用一个URL和处理函数,同时显示视图和处理请求。

创建模板investigate.html

<form action=”/west/investigate2/” method=”post”>
{% csrf_token %}
<input type=”text” name=”staff”>
<input type=”submit” value=”Submit”>
</form>
<p>=================</p>
<p>result:   {{ rlt }}</p>
<p>=================</p>

表格后面还有一个{% csrf_token %}的标签。csrf全称是Cross Site Request Forgery。这是Django提供的防止伪装提交请求的功能。POST方法提交的表格,必须有此标签。

在west/views.py中,用investigate2()来处理表格:

def investigate2(request):
ctx ={}
#ctx.update(csrf(request))
if request.POST:
ctx[‘rlt’] = request.POST[‘staff’]
return render(request, “investigate.html”, ctx)

最终效果如下:

存储数据

提交的数据存入数据库

修改west/views.py的investigate():

def investigate3(request):
    if request.POST:
        submitted  = request.POST[‘staff’]
        new_record = Character(name = submitted)
        new_record.save()
    ctx ={}
    #ctx.update(csrf(request))
    all_records = Character.objects.all()
    ctx[‘staff’] = all_records
    return render(request, “investigate.html”, ctx)

修改模板investigate.html

<form action=”/west/investigate3/” method=”post”>
  {% csrf_token %}
  <input type=”text” name=”staff”>
  <input type=”submit” value=”Submit3″>
</form>
<p>=================</p>
<p>result:</p>
{% for person in staff %}
<p>{{ person }}</p>
{% endfor %}
<p>=================</p>

 

检验数据

Django在获得数据后,可以自动根据该表格对象的要求,对数据进行处理。

修改west/views.py:

from django.template.context_processors import csrf

class CharacterForm(forms.Form):
    name = forms.CharField(max_length = 20)

def investigate3(request):
    ctx ={}
    if request.POST:
        form = CharacterForm(request.POST)
        if form.is_valid():
            submitted  = form.cleaned_data[‘name’]
            new_record = Character(name = submitted)
            new_record.save()

    form = CharacterForm()
    ctx ={}
    ctx.update(csrf(request))
    all_records = Character.objects.all()
    ctx[‘staff’] = all_records
    ctx[‘form’]  = form

    return render(request, “investigate.html”, ctx)

在模板investigate.html中,可以直接显示form对象:

<form action=”/west/investigate3/” method=”post”>
  {% csrf_token %}
  {{ form.as_p }}
  <input type=”submit” value=”Submit3″>
</form>
<p>=================</p>
<p>result:</p>
{% for person in staff %}
<p>{{ person }}</p>
{% endfor %}
<p>=================</p>

效果如下:

sqlite3

OK~~~~~~~~~~~~~~~~~~

 

 

 

发表评论