Python 将绑定结果保存到数据库而不是实际值
这是我第一次使用Django、Python和MSSQL。我正在创建一个简单的网页,在这里我获取一个应用程序的名称,并使用表单将其存储到数据库中 以下是课程: 型号.pyPython 将绑定结果保存到数据库而不是实际值,python,django,django-models,django-forms,Python,Django,Django Models,Django Forms,这是我第一次使用Django、Python和MSSQL。我正在创建一个简单的网页,在这里我获取一个应用程序的名称,并使用表单将其存储到数据库中 以下是课程: 型号.py from django.db import models from django.forms import forms, ModelForm # Create your models here. class Application(models.Model): name = models.CharField(max_
from django.db import models
from django.forms import forms, ModelForm
# Create your models here.
class Application(models.Model):
name = models.CharField(max_length=100)
from django import forms
from .models import Application
# define the class of a form
class ApplicationForm(forms.ModelForm):
class Meta:
# write the name of models for which the form is made
model = Application
# Custom fields
fields = ['name']
from django.shortcuts import render
from .forms import ApplicationForm
def add_application_view(request):
form = ApplicationForm(request.POST or None)
if form.is_valid():
print('Form = ', form) # it shows the html code for debugging purpose
print('cleaned = ', form.cleaned_data['name']) # it shows <bound method Field.clean of <django.forms.fields.CharField object at 0x7fcdd56c3910>>
print(form['name'].value()) # it shows the correct value I entered
print(form.data['name']) # it shows the correct value I entered
form.save()
form = ApplicationForm()
context = {
'form': form
}
return render(request, 'application/add_application.html', context)
forms.py
from django.db import models
from django.forms import forms, ModelForm
# Create your models here.
class Application(models.Model):
name = models.CharField(max_length=100)
from django import forms
from .models import Application
# define the class of a form
class ApplicationForm(forms.ModelForm):
class Meta:
# write the name of models for which the form is made
model = Application
# Custom fields
fields = ['name']
from django.shortcuts import render
from .forms import ApplicationForm
def add_application_view(request):
form = ApplicationForm(request.POST or None)
if form.is_valid():
print('Form = ', form) # it shows the html code for debugging purpose
print('cleaned = ', form.cleaned_data['name']) # it shows <bound method Field.clean of <django.forms.fields.CharField object at 0x7fcdd56c3910>>
print(form['name'].value()) # it shows the correct value I entered
print(form.data['name']) # it shows the correct value I entered
form.save()
form = ApplicationForm()
context = {
'form': form
}
return render(request, 'application/add_application.html', context)
视图.py
from django.db import models
from django.forms import forms, ModelForm
# Create your models here.
class Application(models.Model):
name = models.CharField(max_length=100)
from django import forms
from .models import Application
# define the class of a form
class ApplicationForm(forms.ModelForm):
class Meta:
# write the name of models for which the form is made
model = Application
# Custom fields
fields = ['name']
from django.shortcuts import render
from .forms import ApplicationForm
def add_application_view(request):
form = ApplicationForm(request.POST or None)
if form.is_valid():
print('Form = ', form) # it shows the html code for debugging purpose
print('cleaned = ', form.cleaned_data['name']) # it shows <bound method Field.clean of <django.forms.fields.CharField object at 0x7fcdd56c3910>>
print(form['name'].value()) # it shows the correct value I entered
print(form.data['name']) # it shows the correct value I entered
form.save()
form = ApplicationForm()
context = {
'form': form
}
return render(request, 'application/add_application.html', context)
从django.exe导入渲染
从.forms导入应用程序表单
def添加应用程序视图(请求):
表单=应用程序表单(request.POST或None)
如果form.is_有效():
打印('Form=',Form)#它显示用于调试的html代码
打印('cleaned=',form.cleaned_data['name'])#它显示
打印(表单['name'].value())#它显示了我输入的正确值
打印(form.data['name'])#它显示了我输入的正确值
form.save()
表单=应用程序表单()
上下文={
“形式”:形式
}
返回呈现(请求'application/add_application.html',上下文)
每当我测试页面并输入任何值作为应用程序的名称时,它都会将下面的值作为应用程序的名称存储在数据库中
<bound method Field.clean of <django.forms.fields.CharField object at 0x7fcdd56c3910>>
我在表单中引入了clean()
和save()
函数,但运气不好
我会很感激你的帮助
版本:
Python:3.8
Django:3.1.2
MSSQL:8