Django ModelForm没有保存到数据库中

时间:2018-04-30 05:00:56

标签: django django-models django-forms django-templates django-views

过去几天我一直在努力让我的表单保存到我的数据库中。目前它正在数据库中创建对象,但对象内的所有字段都是空的。我试图将ModelForm中的数据保存到模型中。

Models.py

class ContactForm(models.Model):
Name = models.CharField(max_length= 50)
Email = models.EmailField()
Phone = models.CharField(max_length= 50)
Message = models.CharField(max_length= 200)

def __str__(self):
    return self.Name

forms.py

class ContactForm(ModelForm):
name = forms.CharField(max_length=50)
email = forms.EmailField(required=True)
phone = forms.CharField(max_length=15)
message = forms.CharField(max_length=50)

class Meta:
    model = ContactForm
    fields = ['name','email','phone','message']

views.py

def contact(request):
if request.method=='POST':
    form = ContactForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('/')

else:
    form = ContactForm()

    args = {'form': form}
    return render(request, 'home/contact.html', args)

urls.py

urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^about/$', views.about, name='about'),
url(r'^buy-now/$', views.buyNow, name='buy-now'),
url(r'^buy-form/$', views.buyForm, name='buy-form'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^login/$', login, {'template_name': 'home/login.html'}, name='login'),
url(r'^logout/$', logout, {'template_name': 'home/logout.html'}, name='logout'),
url(r'^account/$', views.account, name='account'),
url(r'^register/$', views.register, name='register'),

模板contact.html



{% extends 'base.html' %}
{% block head %}
    <title>Aeviternus | Contact </title>
{% endblock %}
  </head>
  <body>
{% block body %}
    <!-- Section: Contact -->
    <section>
      <div class="container">
        <div class="row">
          <div class="col s12 m6">
            <div class="card-panel teal white-text center">
              <i class="material-icons">email</i>
              <h5>Contact Us</h5>
              <p>If you have any comments or suggestions for any upcoming content or features you would like to see added, We want to hear about it.</p>
            </div>
            <ul class="collection with-header">
              <li class="collection-header">
                <h4>Location</h4>
              </li>
              <li class="collection-item">CSCI 152 Development Lab</li>
              <li class="collection-item">5241 N Maple Ave</li>
              <li class="collection-item">Fresno, CA 93740</li>
            </ul>
          </div>
          <div class="col s12 m6">
            <div class="card-panel grey lighten-3">
              <h5>Please fill out this form</h5>
              <form method ="post">
                {% csrf_token %}
                  {{ form }}
              <button type="submit" class="waves-effect waves-light btn-large">Submit</button>
            </form>
            </div>
          </div>
        </div>
      </div>
    </section>
{% endblock %}
<!--JavaScript at end of body for optimized loading-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
<script>
  // Sidenav
  const sideNav = document.querySelector('.sidenav');
  M.Sidenav.init(sideNav, {});
</script>
  </body>
</html>
&#13;
&#13;
&#13;

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的型号名称和表单名称都相同。所以,可能是它导致了这个问题。 将表单类名称更改为ContactModelForm。然后你的表单类变为

class ContactModelForm(forms.ModelForm):
  name = forms.CharField(max_length=50)
  email = forms.EmailField(required=True)
  phone = forms.CharField(max_length=15)
  message = forms.CharField(max_length=50)

  class Meta:
    model = ContactForm
    fields = ['name','email','phone','message']

class ContactModelForm(forms.ModelForm):

  class Meta:
    model = ContactForm
    fields = ['Name','Email','Phone','Message']

并在视图中将ContactForm替换为ContactModelForm