- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
虽然标题说问题是“健康”,但事实是它在表格上随机选择一个项目并说这就是问题。
surveys.views.py
from django.shortcuts import render
from surveys.forms import SurveyForm
from django.contrib.auth.decorators import login_required
# Create your views here.
@login_required
def storeBloodData(request):
if request.method == 'POST':
form = SurveyForm(request.POST)
if(form.is_valid()):
cd = form.cleaned_data
bd = SurveyForm (user=cd['user'],
timestamp=cd['timestamp'],
glucose=cd['glucose'],
wellness=cd['wellness'],
weight=cd['weight'],
foodGroups=cd['foodGroups'],
)
bd.save()
print("Saved weather record...")
else:
return render(request,
'bdata_form.html',{'form':form})
print("should be calling status...")
return render(request, 'welcome.html',{'term':"Saved data to d/base..."})
surveys.forms.py
from django import forms
from surveys.models import Survey
from django.conf import settings
FOOD_CHOICE = {
('D','Diary'),
('F','Fruit'),
('G','Grains'),
('M','Meats'),
('V','Vegetables'),
('S','Sweets'),
}
class SurveyForm(forms.ModelForm):
class Meta:
model = Survey
fields = ( 'glucose',
'weight', 'foodGroups',
'wellness', 'user',
'timestamp'
)
surveys.models.py
from django.db import models
from userprofile.models import UserProfile
from django.conf import settings
from django.contrib.auth.models import User
# Create your models here.
FOOD_CHOICE = {
('D','Diary'),
('F','Fruit'),
('G','Grains'),
('M','Meats'),
('V','Vegetables'),
('S','Sweets'),
}
NUM_CHOICE = {
('1','1'),
('2','2'),
('3','3'),
('4','4'),
('5','5'),
('6','6'),
('7','7'),
('8','8'),
('9','9'),
('10','10'),
}
class Survey(models.Model):
user = models.ForeignKey(User, related_name='Survey.user')
glucose = models.DecimalField(max_digits=3, decimal_places=0)
timestamp = models.DateTimeField('date published', default='1990-08-26')
weight = models.DecimalField(max_digits=5, decimal_places=2)
foodGroups = models.CharField(max_length=1)
wellness = models.CharField(max_length=2, choices=NUM_CHOICE, default="1")
def __str__(self):
return self.user
更多信息因此它正确加载表单(并且我能够从管理端添加调查),但每当我尝试从网站端添加它时,它都会抛出错误。
最佳答案
您正在尝试在 is_valid block 内实例化 SurveyForm,而不是调查。
但实际上你不应该尝试这样做,并且你不需要从 clean_data 设置所有这些字段:使用 ModelForm 的全部意义在于你可以只执行 form.save()
它会为您创建并保存一个模型实例。
关于python - __init__() 获得意外的关键字参数 'wellness',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29500012/
为什么正是是 A.__init__() B.__init__() D.__init__() 由以下代码打印?特别是: 为什么是C.__init__() 未打印? 为什么是C.__init__()如果我
目前我有这样的事情: @dataclass(frozen=True) class MyClass: a: str b: str c: str d: Dict[str, str] ...
我正在尝试从父类继承属性: class Human: def __init__(self,name,date_of_birth,gender,nationality): self.name =
如何扩展基类的 __init__,添加更多要解析的参数,而不需要 super().__init__(foo, bar) 在每个派生类中? class Ipsum: """ A base ips
这是我试图解决的一个非常简单的例子: class Test(object): some_dict = {Test: True} 问题是我无法在 Test 仍在定义时引用它 通常,我会这样做:
我在 Objective-C 中使用过这个结构: - (void)init { if (self = [super init]) { // init class }
我有一个类层次结构,其中 class Base 中的 __init__ 执行一些预初始化,然后调用方法 calculate。 calculate 方法在 class Base 中定义,但预计会在派生类
这是我在多种语言中都怀念的一个特性,想知道是否有人知道如何在 Python 中完成它。 我的想法是我有一个基类: class Base(object): def __init__(self):
我正在对 threading.Thread 类进行子类化,它目前看起来像这样: class MyThread(threading.Thread): def __init__(self:
我正在用 cython 写一些代码,我有一些 "Packages “within” modules" . — 这实际上是对我在那里的问题的跟进,结构应该是一样的。问题是这是 cython,所以我处理的
class AppendiveDict(c.OrderedDict): def __init__(self,func,*args): c.OrderedDict.__init_
看完this回答,我明白 __init__ 之外的变量由类的所有实例和 __init__ 内的变量共享每个实例都是唯一的。 我想使用所有实例共享的变量,随机给我的类实例一个唯一的参数。这是我尝试过的较
在下面的代码中: import tkinter as tk class CardShuffling(tk.Tk): background_colour = '#D3D3D3'
我正在覆盖类的 __new__() 方法以返回具有特定 __init__() 集的类实例。 Python 似乎调用类提供的 __init__() 方法而不是特定于实例的方法,尽管 Python 文档在
从内置类型和其他类派生时,内置类型的构造函数似乎没有调用父类(super class)构造函数。这会导致 __init__ 方法不会被 MRO 中内置函数之后的类型调用。 例子: class A:
答: super( BasicElement, self ).__init__() 乙: super( BasicElement, self ).__init__( self ) A 和 B 有什么区
class A(object): def __init__(self): print('A.__init__()') class D(A): def __init__(
到目前为止我已经成功地做了什么: 我创建了一个 elem 类来表示 html 元素(div、html、span、body 等)。 我可以像这样派生这个类来为每个元素创建子类: class elem:
我一直在努力理解 super() 在多重继承的上下文中的行为。我很困惑为什么在 test2.py 的父类中调用 super() 会导致为父类调用 __init__()? test1.py #!/usr
为什么我在 Python 代码中看不到以下内容? class A: def __init__(self, ...): # something important class B
我是一名优秀的程序员,十分优秀!