以GET格式访问数据并发布到结果页面(Django)

时间:2018-02-14 05:42:51

标签: python django

我尝试创建一个表单,该表单接受输入并使用这些输入创建一个发布到结果页面的输出。我到处搜索过,无法了解如何将数据(在下面的情况下,' country'' culture')发布到results_view。

# view.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render

from form.forms import InputForm

def get_list(request):
    if request.method == 'GET':
    form = InputForm(request.GET)
        if form.is_valid():
            country = form.cleaned_data['country']
            culture = form.cleaned_data['culture']

            return results_view(form)

    else:
         form = InputForm()

    return render(request, 'form/index.html', {'form': form})

def results_view(form):
    text = form.restaurant

    c = {'output' : text}
    return render(form, 'form/results.html', c)

# forms.py
from django import forms

class InputForm(forms.Form):
    country = forms.CharField(label='country', max_length=100) 
    cuisine = forms.CharField(label='cuisine', max_length=100)

如何访问输入并将其用作' results_view'中的文字?另外,如果我想将这些结果作为另一个python函数的输入参数传递(比如将国家名称映射到纬度和经度的函数),我该如何将其合并到views.py中?非常感谢!

1 个答案:

答案 0 :(得分:2)

您不需要重定向到另一个功能,只需渲染另一个模板

# view.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render

from form.forms import InputForm

def get_list(request):
    if request.method == 'POST':
    form = InputForm(request.POST)
        if form.is_valid():
            country = form.cleaned_data['country']
            culture = form.cleaned_data['culture']

            c = {'country' : country, 'culture'... whatever you get}
            return render(form, 'form/results.html', c)

    else:
         form = InputForm()

    return render(request, 'form/index.html', {'form': form})