Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)
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)
        
    
    @login_required
    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
                        postToSave.save()
                    else:
                        postToSave.save()
                    return redirect('home3')                
            else:
                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 }}              
                </fieldset>
                <div class="form-group">
                  <button
                    class="btn btn-lg btn-info btn-block text-uppercase"
                    type="submit"
                  >
                    Post</button
                  ><br />
                </div>
              </form>

>Solution :

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

@login_required
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():
            form.save()
            return redirect('home3')
    else:
        form = NewPostForm(instance=post)
    return render(
        request,
        'feed/create_post.html',
        {'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'
        )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading