【问题标题】:Django form based view GET response基于 Django 表单的视图 GET 响应
【发布时间】:2015-11-12 17:58:53
【问题描述】:

为了在网站主页上创建一个表单,我创建了一个表单类和一个请求类。 不知何故,我无法将响应放入 url 查询字符串中。

有人可以帮我解决这个问题吗?

views.py

def index(request):
    if request.method == 'GET':
        form = CurrencyConverterForm(request.GET)
        if form.is_valid():
            base_currency = form.data.__getitem__('base_currency')
            counter_currency = form.data.__getitem__('counter_currency')
            base_amount = form.data.__getitem__('base_amount')

        counter_amount = get_conversion_amount(base_currency, counter_currency, datetime.now(), base_amount)

        response = HttpResponseRedirect('/')
        response['base_currency'] = base_currency
        response['counter_currency'] = counter_currency
        response['base_amount'] = base_amount

        return response
else:
    form = CurrencyConverterForm()

context = {'form': form}

return render(request, '../templates/client/index.html', context)

forms.py

class CurrencyConverterForm(forms.Form):
    base_currency = forms.ModelChoiceField(queryset=Currency.objects.all(), required=True)
    counter_currency = forms.ModelChoiceField(queryset=Currency.objects.all(), required=True)
    base_amount = forms.FloatField(required=True)

urls.py

urlpatterns = [
    url(r'^$', views.index),
    url(r'^admin/', admin.site.urls),

]

index.html

<form action="" method="get">
    {{ form.as_ul }}
    <input type="submit" value="Convert"/>
</form>

【问题讨论】:

  • 在响应集标题字段上设置属性。这就是你想要的吗?
  • 你不需要form.data.__getitem__,你可以访问像form.cleaned_data['base_currency']这样的清理数据。
  • 更一般地说,您不需要直接调用双下划线方法。

标签: python django django-forms django-views


【解决方案1】:

您需要像这样访问表单数据:

data = form.cleaned_data['data']

所以你的视图必须看起来像:

def index(request):
    if request.method == 'GET':
        form = CurrencyConverterForm(request.GET)
        if form.is_valid():
            base_currency = form.cleaned_data['base_currency']
            counter_currency = form.cleaned_data['counter_currency']
            base_amount = form.cleaned_data['base_amount']

        counter_amount = get_conversion_amount(base_currency, counter_currency, datetime.now(), base_amount)

        response = HttpResponseRedirect('/')
        response['base_currency'] = base_currency
        response['counter_currency'] = counter_currency
        response['base_amount'] = base_amount

        return response
else:
    form = CurrencyConverterForm()

context = {'form': form}

return render(request, '../templates/client/index.html', context)

【讨论】:

  • @pythad 感谢您到目前为止的帮助。但是,当我现在点击提交按钮时,查询字符串和字段都是空的。我怎么能解决这些问题?
【解决方案2】:

我已经能够解决它。

我实际上需要的代码比我预期的要少得多。

def index(request):
    counter_amount = ""
    if request.method == 'GET':
        form = CurrencyConverterForm(request.GET)
        if form.is_valid():
            form_base_currency = form.cleaned_data['form_base_currency']
            form_counter_currency = form.cleaned_data['form_counter_currency']
            form_base_amount = form.cleaned_data['form_base_amount']

            # Calculate the counter_amount
            counter_amount = get_conversion_amount(form_base_currency, form_counter_currency, datetime.now(),
                                                   form_base_amount)
            # Retrieve the counter amount from the dict
            counter_amount = counter_amount['GetConversionAmountResult']
            # Maximize the number of decimals to 4
            if counter_amount.as_tuple().exponent < -4:
                counter_amount = "%.4f" % counter_amount

    else:
        form = CurrencyConverterForm()

    context = {
        'form': form,
        'counter_amount': counter_amount
    }

    return render(request, '../templates/client/index.html', context)

【讨论】:

    猜你喜欢
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多