通过djang存储从html到mysql的值

2021-03-06 01:09:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过django将数据发送到mysql数据库。当我运行服务器时,我没有得到任何错误。当我从提交表格时你好.html,这些值不存储在我的数据库中。在数据库里我有桌子你好,继续

在你好.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%}

在模型.py在

^{pr2}$

在表单.py在

from django import forms
from .models import cont

class PostForm(forms.ModelForm):

    class Meta:
        model = cont
        fields = ('name', 'email', 'comments')

在视图.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

})

投票/网址.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'),
]

在网址.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')),

]