未返回HttpResponse对象

未返回HttpResponse对象

我希望将用户重定向到允许他们编辑其帐单详细信息的表单。但是,当我尝试单击链接以转到开票表格时,出现以下错误消息:

ValueError: The view classroom.views.teachers.payment_view didn't return an HttpResponse object. It returned None instead.


这是我的看法:

def payment_view(request):
    user = request.user
    form = MentorProfileForm()
    if request.method == 'POST':
        form = MentorProfileForm(request.POST, request.FILES, instance=user)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('%s' % (reverse('teachers:payment_view')))
        else:
            form = MentorProfileForm(instance=user)
        return render(request, 'classroom/teachers/app-instructor-billing.html', {'form': form})


models.py

class Mentor(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='mentor')
    linkedin = models.URLField(max_length=200,null=True,blank=True)
    photo = models.ImageField(null=True, blank=True, upload_to='media', default='default.jpg')
    address = models.CharField(max_length=500, null=True, blank=True)
    invoice_name = models.CharField(max_length=200, null=False, blank=False)
    account_num = models.IntegerField(default=1234)
    bank_name = models.CharField(max_length=50, null=False)
    branch_code = models.IntegerField(default=1234)

    def __str__(self):
        return "Profile of user {}".format(self.user.username)


forms.py:这些字段显示在注册表单中

class TeacherSignUpForm(UserCreationForm):
    email = forms.EmailField(max_length=100)
    first_name = forms.CharField(max_length=100)
    last_name = forms.CharField(max_length=100)
    linkedin = forms.URLField(max_length=200)
    address = forms.CharField(max_length=500)
    invoice_name = forms.CharField(max_length=200)
    account_num = forms.IntegerField()
    bank_name = forms.CharField(max_length=50)
    branch_code = forms.IntegerField()

    class Meta(UserCreationForm.Meta):
        model = User
        fields = ('email', 'username', 'first_name', 'last_name')

    def save(self, commit=True):
        self.instance.is_teacher = True
        user = super(UserCreationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()
        mentor = Mentor.objects.get_or_create(
            user=user,
            linkedin=self.cleaned_data['linkedin'],
            address=self.cleaned_data['address'],
            invoice_name=self.cleaned_data['invoice_name'],
            account_num=self.cleaned_data['account_num'],
            bank_name=self.cleaned_data['bank_name'],
            branch_code=self.cleaned_data['branch_code'],
        )
        return user


这是模板

<form id="edit-payment-details", method="post", enctype="multipart/form-data" class="form-horizontal">
                        {% csrf_token %}
                      <div class="form-group">
                        <label for="name" class="col-md-2 control-label">Name on Invoice</label>
                        <div class="col-md-6">
                          <div class="form-control-material">
                            <input type="text" class="form-control used" id="name" value="{{ paymentform.invoice_name }}">
                              {{ paymentform.invoice_name }}
                            <label for="invoice_name"></label>
                          </div>
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="address" class="col-md-2 control-label">Address</label>
                        <div class="col-md-6">
                          <div class="form-control-material">
                            <textarea class="form-control used" id="address"> </textarea>
                              {{ paymentform.address }}
                            <label for="address">Address</label>
...

最佳答案

您的else的作用域是if form.is_valid()子句,而不是if request.method == 'POST'子句。您可以使用redirect(..) function [Django-doc]以更方便的方式执行重定向:

from django.shortcuts import redirect

def payment_view(request):
    if request.method == 'POST':
        form = MentorProfileForm(request.POST, request.FILES, instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('teachers:payment_view')
    else:
        form = MentorProfileForm(instance=request.user)
    return render(request, 'classroom/teachers/app-instructor-billing.html', {'form': form})

关于python - View 未返回HttpResponse对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59824049/

10-16 11:14