我正在尝试通过POST请求在数据库中保存表单数据,请求已成功完成但数据未保存在数据库中。
models.py
class Image(models.Model):
user = models.ForeignKey(User, related_name='images')
tagName = models.CharField(max_length=255)
instance = models.CharField(max_length=255)
forms.py
class BuildImageForm(forms.ModelForm):
class Meta:
fields = ('user', 'tagName', 'instance')
model = Image
views.py
class BuildImage(LoginRequiredMixin, CreateView):
form_class = BuildImageForm
model = Image
template_name = 'images/buildImage.html'
success_url = 'user/gui'
def get(self, request, *args, **kwargs):
objectlist = request.user.instances.all()
return render(request, 'images/buildImage.html', {'form': forms.BuildImageForm,
'objectlist': objectlist})
def form_valid(self, form):
instance = form.save()
instance.user = self.request.user
instance.tagName = self.request.tagName
instance.instance = str(self.request.instance_name)
instance.save()
return HttpResponse(status=200)
更新 - 已修改 我已将我的观点更新为:
def post(self, request, *args, **kwargs):
if request.method == 'POST':
form = BuildImageForm(request.POST)
if form.is_valid():
data = form.cleaned_data
form.instance.user = self.request.user
form.instance.tagName = data['tagName']
form.instance.instance = data['instance']
form.save()
else:
print(form.errors)
return HttpResponse(status=200)
现在它在控制台中打印出错误:
<ul class="errorlist"><li>instance<ul class="errorlist"><li>This field is required.</li></ul></li></ul> [23/Jul/2017 04:36:56] "POST /user/images/buildImage/ HTTP/1.1" 200 0
答案 0 :(得分:1)
尝试更改您的视图方法,
def form_valid(self, form):
instance = form.save(commit=False)
instance.user = self.request.user
instance.save()
return HttpResponse(status=200)
答案 1 :(得分:1)
实际上是在添加数据之前保存文件
def form_valid(self, form):
instance = form.save(commit=False)
instance.user = self.request.user
instance.tagName = self.request.tagName
instance.instance = str(self.request.instance_name)
instance.save()
return HttpResponse(status=200)
答案 2 :(得分:0)
尝试为数据库操作设置try和cache 可能是缓存部分缓存了一些错误
<强>更新强>
try:
"save to database code here"
except Exception as e:
print "error here" print e
答案 3 :(得分:0)
我的问题只是通过在我的模板中为例如输入字段提供title属性来解决。
将代码更新为:
<强> buildImage.html 强>
<form class="form-horizontal" method="POST" id="dockerForm">
{% csrf_token %}
<fieldset>
{% if not objectlist %}
<h3><strong>Sorry!</strong> You couldn't have created any Instance yet! <strong>Let's <a href="{% url 'users:create' %}">Create One </a></strong></h3>
{% else %}
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><label for="package">Select Your Instance:</label></span>
<div class="col-md-8">
<select class="form-control" name="instance" id="instance_id" title="Select your Instance">
{% for element in objectlist %}
<option value={{ element.name }}> {{ element.name }} </option>
{% endfor %}
</select>
{% endif %}
</div>
</div>
</fieldset>
</form>
<强> forms.py 强>
class BuildImageForm(forms.ModelForm):
class Meta:
fields = ('tagName', 'instance')
model = Image
<强> views.py 强>
def post(self, request, *args, **kwargs):
if request.method == 'POST':
form = BuildImageForm(request.POST)
if form.is_valid():
data = form.cleaned_data
form.instance.user = self.request.user
form.instance.tagName = data['tagName']
form.instance.instance = data['instance']
form.save()
else:
print(form.errors)
return HttpResponseRedirect(reverse('users:gui'))