gpt4 book ai didi

html - 提交表单 html django

转载 作者:搜寻专家 更新时间:2023-10-30 20:51:48 25 4
gpt4 key购买 nike

我想将数据从我的 html 提交发送到我的数据库 (mysql)。在服务器端,我使用 django(我是初学者)。如果我在表“名称”“电子邮件”“消息”中使用 3 行,你能举个例子吗?当有人按下提交时,我希望将数据存储在数据库中,而其他用户看不到它们。我已经阅读了 djangogirls 和 django tutorial 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>

模型.py

future 导入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)

表单.py

from django import forms

from .models import bosti

class bosti(forms.ModelForm):

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

我的应用程序/urls.py

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

]

我的网站/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')),
]

当我提交表单时,我在我的 url 中获取了值,但它们没有在数据库中传递。

View .py

从 django.shortcuts 导入渲染从 .models 导入 bosti

在此处创建您的 View 。

定义索引(请求): 返回渲染(请求,'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。当我登录数据库时,我看到新表 bosti_bosti。

设置.py

已安装的应用程序:我添加了我的应用

数据库设置,我用的是mysql

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


}
}

最佳答案

<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>

模型.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)

表单.py

from django import forms

from .models import bosti

class bosti(forms.ModelForm):

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

我的应用程序/urls.py

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

]

我的网站/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')),
]

当我提交表单时,我在我的 url 中获取了值,但它们没有在数据库中传递。

View .py

从 django.shortcuts import render from .models import bosti

在此处创建您的 View 。

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',
}


}
}

关于html - 提交表单 html django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39931377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com