ValueError。没有返回HttpResponse

时间:2018-10-31 13:05:13

标签: python django

我遇到此错误

  

视图goal.views.register_course没有返回HttpResponse对象。它返回None。

我不知道我是什么错。

models.py

from django.db import models

class RegisterForCourse(models.Model):
   first_name = models.CharField(max_length=20)
   last_name = models.CharField(max_length=20)
   subject = models.CharField(max_length=12)
   phone = models.CharField(max_length=13)

   def __str__(self):
     return self.subject

forms.py

from django import forms
from .models import RegisterForCourse

class UserRegisterForCourse(forms.ModelForm):
   class Meta:
     model = RegisterForCourse
     fields = ['first_name', 'last_name', 'subject', 'phone']

view.py

def register_course(request):
if request.method == 'POST':
    form = UserRegisterForCourse(request.POST)
    if form.is_valid():
        form.save()
        subject = form.cleaned_data.get('subject')
        messages.success(request, f'You have been successfully registered for {subject}')
        return redirect('index')
else:
    form = UserRegisterForCourse()
    return render(request, 'goal/index.html', form)

在模板中

<form action="{% url 'register' %}" method="post" accept-charset="utf-8">
        {% csrf_token %}
        <input type="text" placeholder="First Name" required>
        <input type="text" placeholder="Last Name" required>
        <input type="text" placeholder="Subject" required>
        <input type="text" placeholder="Phone Number" required>
        <button type="submit" class="btn" id="btn">Submit</button>
    </form>

您能帮我解决这个问题吗?预先感谢!

0 个答案:

没有答案