没有返回HttpResponse对象。它改为返回None - django

时间:2017-09-04 00:19:59

标签: python django django-views

我收到一个错误,我有一个观点,我想知道是否有人可以帮我弄清楚它来自哪里。我很确定它是小的,我没有看到它来自哪里......

在视图中,将显示一个表单供用户输入信息,一旦提交表单,它就会被处理,然后重定向到用户家...

这是错误:

ValueError at /transfer/
The view tab.views.transfers didn't return an HttpResponse object. It returned None instead.
Request Method: POST
Request URL:    http://localhost:8000/transfer/
Django Version: 1.8.6
Exception Type: ValueError
Exception Value:    
The view tab.views.transfers didn't return an HttpResponse object. It returned None instead.

这是views.py

def transfers(request):
    if 'username' not in request.session:
        return redirect('login')
    else:
        username = request.session['username']
        currentUser = User.objects.get(username = username)
        if request.method == 'POST':
            form = TransferForm(request.POST)
            if form.is_valid():
                cd = form.cleaned_data
                from_acct = cd['from_acct']
                to_acct = cd['to_acct']
                amount = cd['amount']
                memo = cd['memo']
                new_transfer = Transfers.objects.create(
                    user = currentUser,
                    from_acct = from_acct,
                    to_acct = to_acct,
                    amount = amount,
                    memo = memo,
                    frequency = 1,
                    status = 1,
                )
                return redirect('home_page')
        else:
            form = TransferForm()
            form.fields['from_acct'].queryset = Accounts.objects.filter(user = currentUser).all()
            message = 'please fill out the below form'
            parameters = {
                'form':form,
                'currentUser':currentUser,
                'message':message,
            }
            return render(request, 'tabs/user_balance.html', parameters)

这是html文件:

{% extends "base.html" %}

{% block content %}
  <h1>Transfer Money</h1>
  {% if message %}
    <p>{{message}}</p>
  {% endif %}
  <form action="." method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" name="submit" value="submit">
  </form>
{% endblock %}

这是forms.py文件部分

class TransferForm(forms.ModelForm):
    acct_choices = (('tabz', 'Tabz - Username'),
                    ('Wells Fargo', 'Wells Fargo - Username'))
    from_acct = forms.TypedChoiceField(
        choices=acct_choices, widget=forms.RadioSelect, coerce=int
    )
    to_acct = forms.TypedChoiceField(
        choices=acct_choices, widget=forms.RadioSelect, coerce=int
    )
    class Meta:
        model = Transfers
        fields = ['from_acct', 'to_acct', 'amount', 'memo']
        labels = {
            'from_acct':'from',
            'to_acct':'to',
        }

2 个答案:

答案 0 :(得分:1)

    from django.http import HttpResponse, HttpResponseRedirect
    if request.method == 'POST':
        form = TransferForm(request.POST)
        if form.is_valid():
            ...
            return HttpResponseRedirect(reverse_lazy('home'))
        else:
            form.fields['from_acct'].queryset = Accounts.objects.filter(user = currentUser).all()
            message = 'please fill out the below form'
            parameters = {
            'form':form,
            'currentUser':currentUser,
            'message':message,
            }
            return render(request, 'tabs/user_balance.html', parameters)

html add form.errors

{% extends "base.html" %}

{% block content %}
  <h1>Transfer Money</h1>
  {% if message %}
    <p>{{message}}</p>
  {% endif %}
  <form action='your_url/' method="POST">
    {% csrf_token %}
    {{ field.errors }}
    {{ form.as_p }}
    <input type="submit" name="submit" value="submit">
  </form>
{% endblock %}

答案 1 :(得分:1)

好吧,应该抛出此错误只是因为您向视图提供了无效的表单。如果你看一下视图的逻辑,如果它是一个POST并且表单无效,那么视图不会返回任何内容......对于python,没有。这是你正确的错误? 尝试在return redirect('home_page')之后添加一个带有return的else语句,看看这是否修复了这部分。