UpdateView wont databind the from properties

class PostForNewsFeed(models.Model):
    post = models.CharField(max_length=50, choices=POSTTYPE_CHOICES, default='Just a Mesage')

    title = models.CharField(max_length=100, blank=False, null=False)
    content = models.TextField(default=None)

    pic = models.ImageField(upload_to='path/to/img', default=None,blank=True)
tags = models.CharField(max_length=100, blank=True)

    class NewPostForm(forms.Form):
        post = forms.ChoiceField(choices = POSTTYPE_CHOICES)
        title = forms.CharField(max_length=100)
        content = forms.CharField(widget=SummernoteWidget())  # instead of forms.Textarea
        pic = forms.ImageField(required=False)
        tags = forms.CharField(max_length=200,required=False)
    def update_post(request,pk):
        user = request.user
        postToSave = get_object_or_404(PostForNewsFeed, pk=pk)
        form = NewPostForm(request.POST, request.FILES)
        if user.is_authenticated:
            if request.method == "POST":                            
                if form.is_valid():
                    post = request.POST['post']
                    title = request.POST['title']
                    content = request.POST['content']               
                    tags = request.POST['tags']
                    postToSave.post = post
                    postToSave.title = title
                    postToSave.content = content
                    postToSave.tags = tags                              
                    if 'pic' in request.FILES:
                        pic = request.FILES['pic']
                        postToSave.pic = pic
                    return redirect('home3')                
                form = NewPostForm()            
        return render(request, 'feed/create_post.html', {'form':form,'page_title': 'Share a Post' })
    <form class="form-signin" method="POST" enctype="multipart/form-data">
                {% csrf_token %}
                <fieldset class="form-group">
                  <br />
                  {{ form |crispy }}              
                <div class="form-group">
                    class="btn btn-lg btn-info btn-block text-uppercase"
                  ><br />

>Solution :

Please use a ModelForm. Indeed, we can work with:

class NewPostForm(forms.ModelForm):
    class Meta:
        model = PostForNewsFeed
        fields = ('post', 'title', 'content', 'pic', 'tags')
        widgets = {'content': SummernoteWidget()}

This makes it convenient to let the form handle this:

def update_post(request, pk):
    post = get_object_or_404(PostForNewsFeed, pk=pk)
    if request.method == 'POST':
        form = NewPostForm(request.POST, request.FILES, instance=post)
        if form.is_valid():
            return redirect('home3')
        form = NewPostForm(instance=post)
    return render(
        {'form': form, 'page_title': 'Share a Post'},

This can even easily be put into a class-based view:

from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lay
from django.views.generic import UpdateView

class UpdatePostView(LoginRequiredMixin, UpdateView):
    model = PostForNewsfeed
    form_class = NewPostForm
    success_url = reverse_lazy('home3')

    def get_context_data(self, *args, **kwargs):
        return super().get_context_data(
            *args, **kwargs, page_title='Share a Post'

Leave a Reply