我想将数据从我的html提交发送到我的数据库(mysql)。 在服务器端我使用django(im begginer)。 如果我在表格中使用3行,你能举例说明吗?#34; name" "电子邮件" "消息&#34 ;. 当有人按提交时,我希望数据存储在数据库中,其他用户无法看到它们。我已经阅读了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
来自将来导入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')),
]
当我提交表单时,我在我的网址中获取了值,但它们并未在DB中传递。
views.py
来自django.shortcuts导入渲染 来自.models import bosti
def index(请求): return render(request,&#39; blog / hello.html&#39;,{})
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()
我有模特老板。当我在db中登录时,我看到新表bosti_bosti。
settings.py
已安装的应用: 我添加了myapp
数据库设置,我使用mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'myDb',
'USER': 'myuser',
'PASSWORD': 'mypassword'
}
}
答案 0 :(得分:0)
<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')),
]
当我提交表单时,我在我的网址中获取了值,但它们并未在DB中传递。
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',
}
}
}