DJANGO - 使用表单数据重定向到不同的页面

时间:2017-07-01 23:40:17

标签: django python-2.7 django-forms django-templates django-views

您好我想使用from数据重定向到目标网页。例如,当用户填写表格中输入的数据时,我希望在目的地页面上输出该数据 我的代码如下: -

源页面(experiment.html),我不确定该表单的操作应该是什么,所以请帮助我

<form action="{% url 'lazer.views.about_experiment' exp.link_name %}" method="POST"> 
  {% csrf_token %}
      <label>Researcher Name(s):<input type="text" name="researcher">
    <lable>Study Summary<textarea rows="10" cols="50" placeholder="here you go" maxlength="500" class="form-control" name="study"></textarea>
      <br>
      <input type = "submit" value="Submit" class="btn btn-primary" />
  </form>

目标网页(about_experiment.html)

<h3>Holding page for {{ exp.name }}.</h3>
<h2> {{ form }} </h2>

views.py

from .forms import AboutHelp
from django.shortcuts import render
from django.http import HttpResponseRedirect

def about_experiment(request):

if request.method == 'POST':
    form = AboutHelp(request.POST)
    if form.is_valid():
        researcher = form.cleaned_data['researcher']
        study = form.cleaned_data['study']

else:
    form = AboutHelp()
return render(request, 'about_experiment.html', {'form': form})`

forms.py

from django import forms

class AboutHelp(forms.Form):
    researcher  = forms.CharField(max_length=100)
    study = forms.CharField(max_length=500)

urls.py

url(r'^about/(?P<ex_link_name>\w+)', lazer.views.about_experiment, name='lazer.views.about_experiment'),

0 个答案:

没有答案