我想通过django将数据发送到我的mysql数据库。当我运行服务器时,我没有收到任何错误。当我从hello.html提交表单时,值不会存储在我的数据库中。在DB中我得到了表hello_cont
hello.html的
{% block content %}
<form action="/polls/" method="POST"> {% csrf_token %}
<input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
</div>
</div>
<textarea class="form-control" id="comments" name="comments" placeholder="Comment" rows="5"></textarea><br>
<div class="row">
<div class="col-sm-12 form-group">
<button class="btn btn-default pull-right" type="submit">Send</button>
</form>
{%endblock%}
models.py
from __future__ import unicode_literals
from django.db import models
class cont(models.Model):
name = models.CharField(max_length=10)
email = models.CharField(max_length=10)
comments= models.CharField(max_length=250)
def __str__(self):
return self.name
forms.py
from django import forms
from .models import cont
class PostForm(forms.ModelForm):
class Meta:
model = cont
fields = ('name', 'email', 'comments')
view.py
from django.utils import timezone
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import cont
from .forms import PostForm
def hello(request):
return render(request, 'polls/hello.html')
def post_new(request):
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.name = request.user
post.email = request.email
post.comments = request.comments
post.save()
else:
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form
})
轮询/ urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
url(r'^post/new/$', views.post_new, name='post_new'),
]
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from polls.views import hello
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('polls.urls')),