目标是创建一个网页,使人们可以拍照并提交有关图片的元数据。因为同一元数据可能有多张图片,所以我像这样设置模型:
from django.db import models
class MetaData(models.Model):
description = models.CharField(max_length=5000)
class Image(models.Model):
image = models.ImageField()
metadata_id = models.ForeignKey(
MetaData, on_delete=models.DO_NOTHING, related_name='image_to_metadata'
)
使用模型创建了简单的表单:
from django.forms import ModelForm
from django.forms.models import inlineformset_factory
from .models import MetaData, Image
class MetaDataForm(ModelForm):
class Meta:
model = MetaData
fields = '__all__'
class ImageForm(ModelForm):
class Meta:
model = Image
fields = ('image',)
MetaDataImageFormSet = inlineformset_factory(MetaData, Image, fields=('image',), extra=1)
然后使用以下视图显示它们并根据以下指南Django inline formsets with class based views
进行身份验证from django.views.generic import CreateView
from django.db import transaction
from .forms import MetaDataForm, MetaDataImageFormSet
from .models import MetaData
class CaptureView(CreateView):
model = MetaData
template_name = 'pages/capture.html'
form_class = MetaDataForm
success_url = '/'
def get_context_data(self, **kwargs):
data = super(CaptureView, self).get_context_data(**kwargs)
if self.request.POST:
data['images'] = MetaDataFormSet(self.request.POST)
else:
data['images'] = MetaDataFormSet()
return data
def form_valid(self, form):
context = self.get_context_data()
images = context['images']
with transaction.atomic():
self.object = form.save()
if images.is_valid():
images.instance = self.object
images.save()
return super(CaptureView, self).form_valid(form)
from django.urls import path
urlpatterns = [
path("", CaptureView.as_view(), name="capture"),
]
# Hiding django toolbar paths and error page handling
当我尝试发布图像时,出现以下错误消息
_execute中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ backends \ utils.py” 85. return self.cursor.execute(sql,params)
上述异常(“ id”列中的空值违反了非空约束 详细信息:失败行包含(null,test1)。 )是以下异常的直接原因:
内部文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ core \ handlers \ exception.py” 34. response = get_response(request)
_get_response中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ core \ handlers \ base.py” 126. response = self.process_exception_by_middleware(e,request)
_get_response中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ core \ handlers \ base.py” 124. response = wrapd_callback(request,* callback_args,** callback_kwargs)
内部的文件“ C:\ Users \ dbutler \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ contextlib.py” 74. return func(* args,** kwds)
视图中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ views \ generic \ base.py” 68. return self.dispatch(request,* args,** kwargs)
调度中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ views \ generic \ base.py” 88. return handler(请求,* args,** kwargs)
后文中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ views \ generic \ edit.py” 172. return super()。post(request,* args,** kwargs)
后文中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ views \ generic \ edit.py” 142. return self.form_valid(form)
form_valid中的文件“ C:\ code \ capture_version_2 \ receipts \ views.py” 27. self.object = form.save()
保存文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ forms \ models.py” 458. self.instance.save()
保存文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ models \ base.py” 718. force_update = force_update,update_fields = update_fields)
save_base中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ models \ base.py” 748.更新= self._save_table(原始,cls,force_insert,force_update,使用,update_fields)
_save_table中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ models \ base.py” 831. result = self._do_insert(cls._base_manager,using,fields,update_pk,raw)
_do_insert中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ models \ base.py” 869. using = using,raw = raw)
manager_method中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ models \ manager.py” 82. return getattr(self.get_queryset(),name)(* args,** kwargs)
_insert中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ models \ query.py” 1136. return query.get_compiler(using = using).execute_sql(return_id)
execute_sql中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ models \ sql \ compiler.py” 1289. cursor.execute(sql,params)
在执行文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ debug_toolbar \ panels \ sql \ tracking.py” 186. return self._record(self.cursor.execute,sql,params)
_record中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ debug_toolbar \ panels \ sql \ tracking.py” 124.返回方法(sql,params)
在执行文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ backends \ utils.py” 100. return super()。execute(sql,params)
在执行文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ backends \ utils.py” 68. return self._execute_with_wrappers(sql,params,many = False,executor = self._execute)
_execute_with_wrappers中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ backends \ utils.py” 77. return executor(sql,params,many,context)
_execute中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ backends \ utils.py” 85. return self.cursor.execute(sql,params)
退出中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ utils.py” 89.从exc_value提高dj_exc_value.with_traceback(traceback)
_execute中的文件“ C:\ Users \ dbutler \ capture_version_2 \ lib \ site-packages \ django \ db \ backends \ utils.py” 85. return self.cursor.execute(sql,params)
异常类型:/的IntegrityError 异常值:“ id”列中的空值违反了非空约束 详细信息:失败行包含(null,test1)。