我搜索了20到30个帖子,但没有找到任何有用的内容。我是通过从视图中的HTML文件或表单中选择值来将数据存储到数据库。请告诉我如何实现这一目标。我错过了什么代码。在此先感谢您的帮助。
view.py
def blog_list(request):
form = AttendanceForm()
if request.method == "POST":
form1 = AttendanceForm(request.POST)
if form1.is_valid:
form1.save()
return render(request, 'blog/blog_list.html',{
'form1':form1,
})
return render(request, 'blog/blog_list.html',{
'form':form,
})
my forms.py
class AttendanceForm(forms.ModelForm):
action = forms.ModelChoiceField(queryset=Action.objects.all(), empty_label="-----------", required=True)
employee = forms.ModelChoiceField(queryset=Employee.objects.all(), empty_label="-----------", required=True)
class Meta:
model = Action
fields = ['employee','action']
我的model.py
class Action(models.Model):
action_name = models.CharField(max_length = 100)
def __str__(self):
return self.action_name
class Employee(models.Model):
employee_id = models.AutoField(primary_key = True)
employee_name = models.CharField(max_length = 100)
def __str__(self):
return self.employee_name
class Attendance(models.Model):
u = models.ForeignKey(Employee)
action = models.ForeignKey(Action)
action_time = models.DateTimeField(default=timezone.now())
答案 0 :(得分:2)
在Python中,您需要使用括号来调用方法。
if form.is_valid():
form.save()
答案 1 :(得分:0)
在Model:
中进行这些更改后,它正常工作import urllib2
import json
API_KEY='key'
f = urllib2.urlopen('http://api.wunderground.com/api/'
+ API_KEY + '/geolookup/conditions/q/IA/Cedar_Rapids.json')
# Saving the below variable into a .json file gives all the JSON data
# on a single line
json_string = f.read()
with open('data.json', 'w') as outfile:
json.dump(json_string, outfile, indent=4, sort_keys=True, separators=(",", ':'))