自动填充Django Summernote

时间:2018-11-16 13:20:28

标签: python django summernote

我正在提交一个带有summernote小部件的表单。

我的forms.py:

 from django import forms
 from .models import Task,Images
 from django_summernote.widgets import SummernoteWidget

 class StudentTaskForm(forms.ModelForm):
   title = forms.CharField(widget=forms.TextInput(attrs={'class': 
  'form-control',' type': "text",'placeholder':'Enter Title'}))
   content = forms.CharField(widget=SummernoteWidget())


   class Meta:
     model = Task
     fields = [
        'title',
        'content',

     ]
     widgets = {
        'content': SummernoteWidget(),
    }

 def clean_object(self):
    instance = self.instance

class ImageForm(forms.ModelForm):
 image = forms.ImageField(label='Image')
  class Meta:
    model = Images
    fields = ('image', )

ImageForm是此Taskform的表单集部分,并且表单提交成功进行。我希望用户能够编辑任务(如果该任务已经存在)。为此,我希望summernote小部件预先填充先前输入的数据(标题,内容,图像)。我可以实现此方法吗?

我认为:

@login_required(login_url='/account/login/')
def TaskCreateView(request,pk,todo_id):
  if not request.user.is_authenticated:
    return redirect('accounts:index')
  elif User.objects.filter(pk=request.user.pk, 
  mentor__isnull=True).exists():
    instance = get_object_or_404(Level, pk=pk)
    sweetify.error(request, 'You have not added a trainer yet')


    return HttpResponseRedirect(instance.get_absolute_url())
 else  :
    instance = get_object_or_404(Level, pk=pk)
    qs = instance.todo_set.get(id=todo_id)

    todo = Task.objects.filter(todo=qs, student=request.user)
    print("TODOS",todo)
    verification = Verification.objects
    .filter(task__in=todo,is_accepted=True,is_rejected=False)
    bug=False
    for tod in todo:
      if tod.is_verified==False:
         bug=True
    if todo.exists() and verification.exists():
      messages.error(request, 'You already completed this task')
      return redirect('student:level-detail', pk=instance.id)

    elif bug==True:
     messages.error(request, 'You already completed this task')
     return redirect('student:level-detail', pk=instance.id)

form = StudentTaskForm(request.POST or None, request.FILES or None)

if form.is_valid():
    form.instance.user = User.objects.get(id=request.user.id)

    obj = form.save(commit=False)
    obj.student = request.user
    obj.todo = qs
    obj.level = instance
    obj.save()



ImageFormSet = inlineformset_factory(Task,Images,
                                        form=ImageForm,min_num=0,
                                 max_num=3,  
 validate_min=True,extra=3)

if request.method == 'POST':
    formset = ImageFormSet(request.POST, request.FILES,
                           instance=obj)
    if formset.is_valid():
       formset.save()

       return redirect('student:dashboard')
else:
    formset = ImageFormSet(queryset=Images.objects.none())


context={
    'form': form,
    "qs": qs,
    'todo':todo,
    'formset': formset,
    'verified': verification,
 }


return render(request,'task_form.html',context)

0 个答案:

没有答案