試試這個設置: 這是您的model.py
class Post(models.Model):
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250)
body = models.TextField()
is_activate = models.BooleanField(default=False)
和view.py
from foldername import Post //if there are subfolders use'.' between them.
//import the other packages which u need
form=form_name //your form name
template=template_name //your template name
def post(self, request, *args, **kwargs):
try:
form = self.form_class(request.POST)
if form.is_valid():
new_formfile = form.save(commit=False)
new_formfile.is_activate = true
new_formfile.save()
except Exception as e:
return render(request, 'exception.html', {'exception': str(e), 'message': e.message})
OR
//在try塊
id = request.GET.get('id')
id_instance = Post.objects.get(pk = id)
form = self.form_class(request.POST, instance = id_instance)
if form.is_valid():
new_formfile = form.save(commit=False)
new_formfile.is_activate = true
new_formfile.save()
form.save()
你有什麼能夠做的另一種方法寫,在你的意見? –
這裏有什麼複雜的?爲什麼更改'is_activate'字段與更改任何其他字段不同? –
@EvansMurithi因爲我需要在模板中使用buttom而不是通過管理員 –