提交表格html django

我想将我的html提交数据发送到我的数据库(mysql) . 在服务器端我使用django(im begginer) . 如果我在表格“name”“email”“message”中使用3行,你能举例说明吗?当有人按提交时,我希望数据存储在数据库中,其他用户无法看到它们 . 我已经阅读了djangogirls和django教程1.10,我已经制作了一个webapp . 但如果我想自己做点什么,我就会陷入困境 .

我在index.html中的表单

<form method="POST" class="post-form" action="/myapp/">{% csrf_token %}{{form}}
      <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="mail" type="email" required>
    </div>
  </div>
  <textarea class="form-control" id="message" name="message" placeholder="Comm" rows="5"></textarea><br>
  <div class="row">
    <div class="col-sm-12 form-group">
{{ form.as_p }}
      <button class="btn btn-default pull-right" type="submit">Send</button>

    </form>

models.py

来自 future import unicode_literals

来自django.db导入模型

# Create your models here.
class bosti(models.Model):

   name = models.CharField(max_length = 50)
   email = models.CharField(max_length = 50)
   message = models.CharField(max_length = 50)

forms.py

from django import forms

from .models import bosti

class bosti(forms.ModelForm):

    class Meta:
        model = Post
        fields = ('id', 'name', 'email',' message')

MYAPP / urls.py

from django.conf.urls import url
    from . import views
    urlpatterns = [
        url(r'^$', views.index, name='hello.html'),

]

mysite的/ urls.py

from django.conf.urls import url, include
from django.contrib import admin

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^myapp/', include('myapp.urls')),
    ]

当我提交表单时,我会在我的网址中获取值,但它们并未通过数据库传递 .

views.py

来自django.shortcuts从.models import bosti导入渲染

在此处创建您的视图 .

def index(request):return render(request,'blog / hello.html',{})

def bosti(request,
    template = 'blog/hello.html', *args, **kwargs):
    if request.user.is_authenticated():
       name= request.GET.get('name')
       email= request.GET.get('email')
       message= request.GET.get('message')
       Model = bosti(name = name, email = email, message = message)

       Model.save()

我有模特bosti . 当我在db中登录时,我看到新表bosti_bosti .

settings.py

已安装的应用:我添加了myapp

数据库设置,即时通讯使用mysql

DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME':'myDb',
            'USER': 'myuser',
            'PASSWORD': 'mypassword'


        }
    }

回答(1)

3 years ago

<form method="POST" class="post-form" action="/myapp/">
      <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="mail" type="email" required>
    </div>
  </div>
  <textarea class="form-control" id="message" name="message" placeholder="Comm" 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>

models.py

from future import unicode_literals

from django.db import models
# Create your models here.
class bosti(models.Model):

   name = models.CharField(max_length = 50)
   email = models.CharField(max_length = 50)
   message = models.CharField(max_length = 50)

forms.py

from django import forms

from .models import bosti

class bosti(forms.ModelForm):

    class Meta:
        model = Post
        fields = ('id', 'name', 'email',' message')

MYAPP / urls.py

from django.conf.urls import url
    from . import views
    urlpatterns = [
        url(r'^$', views.index, name='hello.html'),

]

mysite的/ urls.py

from django.conf.urls import url, include
from django.contrib import admin

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^myapp/', include('myapp.urls')),
    ]

当我提交表单时,我会在我的网址中获取值,但它们并未通过数据库传递 .

views.py

来自django.shortcuts从.models import bosti导入渲染

在此处创建您的视图 .

def index(request): return render(request, 'blog/hello.html', {})

def bosti(request,
    template = 'blog/hello.html', *args, **kwargs):
    if request.user.is_authenticated():
       name= request.GET.get('name')
       email= request.GET.get('email')
       message= request.GET.get('message')
       Model = bosti(name = name, email = email, message = message)

       Model.save()

将此选项添加到数据库设置:

DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME':'myDb',
            'USER': 'myuser',
            'PASSWORD': 'mypassword'
            'OPTIONS': {
                'sql_mode': 'traditional',
            }


        }
    }