hii Я новичок в Django, поэтому я работаю над своим первым блогом, который является блогом articals, поэтому я пытаюсь сохранить статьи из шаблона, используя формы Django, но при сохранении ошибки «объект bool» не имеет атрибута «_committed», который ищетрешение, но не нашел никакого решения. Пожалуйста, помогите мне.
Models.py:
class post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
title = models.CharField(max_length=20)
discription=models.CharField(default=None ,max_length=100)
text = models.TextField(max_length=2000)
image=models.ImageField(upload_to="blog/",default=True,null=True,blank=True)
created_date = models.DateTimeField(default=timezone.now())
published_date=models.DateTimeField(blank=True , null=True)
def publish(self):
self.published_date=timezone.now()
self.save()
def __str__(self):
return self.title
forms.py:
from django import forms
from .models import post
class post_form(forms.ModelForm):
class Meta:
model=post
fields=[
'title','discription','text','image'
]
views.py:
def post_new(request,*args,**kwargs):
if request.method == 'POST':
form=post_form(request.POST,request.FILES)
if form.is_valid():
p=form.save(commit=False)
p.author=request.user
p.published_date=timezone.now()
p.save()
return redirect("/post_list")
else:
form = post_form()
context = {
'form': form
}
return render(request,'blog/post_new.html',context)
Traceback:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/post_new
Django Version: 2.2.3
Python Version: 3.7.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e,
request)
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "C:\Users\lenovo\Desktop\blogproject\mysite\blog\views.py" in
post_new
39. p.save()
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\base.py" in save
741. force_update=force_update,
update_fields=update_fields)
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\base.py" in save_base
779. force_update, using, update_fields,
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\base.py" in _save_table
870. result = self._do_insert(cls._base_manager, using,
fields, update_pk, raw)
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\base.py" in _do_insert
908. using=using, raw=raw)
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args,
**kwargs)
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\query.py" in _insert
1186. return
query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\sql\compiler.py" in execute_sql
1334. for sql, params in self.as_sql():
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\sql\compiler.py" in as_sql
1278. for obj in self.query.objs
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\sql\compiler.py" in <listcomp>
1278. for obj in self.query.objs
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\sql\compiler.py" in <listcomp>
1277. [self.prepare_value(field, self.pre_save_val(field,
obj)) for field in fields]
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\sql\compiler.py" in pre_save_val
1228. return field.pre_save(obj, add=True)
File "C:\Users\lenovo\Envs\blogproject\lib\site-
packages\django\db\models\fields\files.py" in pre_save
286. if file and not file._committed:
Exception Type: AttributeError at /post_new
Exception Value: 'bool' object has no attribute '_committed'