featured_imageThis ...
 
Notifications
Clear all

featured_imageThis field is required.

5 Posts
2 Users
0 Reactions
21 Views
0
Topic starter

Reference Section# 20

Add post assign logged in user to author

Sir, I am trying to add a post but the following bug appearing. I tried my best to debug it but failed. kindly help in this regard.

models.py 

classBlog(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=150,blank=True,unique=True)
    category = models.ForeignKey(Category,on_delete=models.CASCADE)
    auther = models.ForeignKey(User,on_delete=models.CASCADE ,null=True)
    featured_image = models.ImageField(upload_to='uploads/%y/%m/%d')
 
add_post.html
 
<form action= "{% url 'add_post' %}"method = "POST"enctype="multipart/form-data">
 
urls.py
 
path('posts/add/', views.add_post,name='add_post'),
 
 
views.py
 
def add_post(request):
    if request.method == 'POST':
        form = BlogPostForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect('posts')
        else:
            print('form is invalid')
            print(form.errors)
    form=BlogPostForm()
    context={
        'form': form
    }
    return render(request, 'dashboard/add_post.html', context)

 

3 Answers
0

Ejaz Ali Inayat,what issue are you actually facing? Could you provide us with a screenshot of your issue? This will help us provide you with a proper solution.

Ejaz Ali Inayat Ejaz Ali Inayat Topic starter 14/02/2025 9:08 pm

@mohaiminul-islam Sir, I have attached the screenshot of the issue

0

Hi Ejaz Ali Inayat,

The issue you are facing occurs because, when you try to create a blog, the feature image is not receiving any input. This can happen for several reasons:

  1. In your forms.py, the feature image field may not be correctly added.

  2. It can also happen if you have not included enctype="multipart/form-data" in your form tag within the template.

Please check these issues. If you are still unable to fix them, kindly share your updated GitHub repository with us.

Ejaz Ali Inayat Ejaz Ali Inayat Topic starter 18/02/2025 1:47 am

@mohaiminul-islam Sir, the following error is appearing.

form is invalid

  • featured_image
    • This field is required.

https://github.com/webextolcollege/django-blog.git

0

 Ejaz Ali Inayat,

I think you did not try my previous answer. Earlier, in point number 2, I mentioned that you need to include enctype="multipart/form-data" in your form tag. However, I did not see this in your code. I have added it, and your issue is now resolved. Here is the full code:

<form action="{% url 'add_post' %}" method="POST" enctype="multipart/form-data">
          {% csrf_token %}
          {{ form|crispy }}
         <button type="submit" class="btn btn-warning">Submit</button>
</form>

Please watch the videos carefully. If you face any issues, ask in the Q&A section and make sure to follow the steps we provided to resolve the issue.

 

Share: