我正在尝试通过表单向数据库添加对象,但没有任何反应,信息不会出现在db中。这是我的代码,我已经搜索了几个小时,什么都没找到
models.py
from django.db import models
import os
from django import forms
from django.contrib.auth.models import User
class Projects(models.Model):
Cascades = []
for d, dirs, files in os.walk('pd/haarcascades'):
for f in files:
path = os.path.abspath(f) # формирование адреса
name = os.path.splitext(f)[0]
file_name_path = ()
file_name_path = (path, name)
Cascades.append(file_name_path)
ProjectName = models.CharField(max_length = 15)
ProjectOwner = models.OneToOneField(User, primary_key = True) #
ProjectType = models.CharField(max_length = 1) # D for Detection , C for Creating Own Cascacde
CascadeType = models.CharField(max_length = 150, choices=Cascades)
CascadePath = models.CharField(max_length = 150, null = True)
ProjectPath = models.CharField(max_length = 100)# for later downloading
ProjectReadyFilter = models.BooleanField(default=False)# 0 uncompleted project, 1 - completed
forms.py
from django import forms
import os
from .models import Projects
class ChosingCascade(forms.ModelForm):
class Meta:
model = Projects
fields = ('ProjectName', 'CascadeType',)
views.py
class PhotoDetectionProcessView(FormView):
template_name = "pd/ChosingCascade.html"
success_url = "/pd/ChosingCascade"
form_class = ChosingCascade
def ChosingCascade(request):
def get(self, request):
form_class = ChosingCascade()
return render(request, self.template_name, {'form_class':form_class})
def post(self,request):
form = ChosingCascade(request.POST)
if form.is_valid():
post = form.save(commit = False)
UserName = request.user
post.ProjectOwner = request.user
post.ProjectName = form.cleaned_data.get("ProjectName")
post.CascadeType = CascadeType(initial={'origin':ChosingCascade.Cascades[1]})
post.CascadePath = CascadeType(initial={'origin':ChosingCascade.Cascades[0]})
post.ProjectType = 'D'
os.makedirs('users/%s/PhotoDetection/%s', UserName, ProjectName)
post.ProjectPath = ('users/%s/PhotoDetection/%s', UserName, ProjectName)
post.save()
form= ChosingCascade()
return redirect('pd/ChosingCascade: pd/ChosingCascade')
args = {'form':form }
return render(request, self.template_name, args)
ChosingCascade.html
<html>
<head>
<meta charset="UTF-8">
<title>Chosing Cascade</title>
</head>
<body>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Vote" />
</form>
</body>
</html>
这个想法是关注:用户创建项目,输入名称并选择haarcascade,但是当我按下&#34;投票&#34; db
中没有数据出现