Click here to Skip to main content
15,443,018 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am working on a Social Media Website, the base code is from an Open Source Project on GitHub.
On the base project you can only post text.

I was looking around the Internet and found a instruction to implement img upload.

But it doesn't work, because i get a lot of error codes.

Error Code:

AttributeError: module 'django.forms' has no attribute 'Post'

my Code:

from django import forms
    from django.forms import fields
    from .models import Post, Comment

    class Post(forms.Post): 
        class Meta: 
            model = Post
            fields = ['name', 'Main_Img']

    """ Home page with all posts """
    def first(request):
        context = {
        return render(request, 'blog/first.html', context)
    """ Posts of following user profiles """
    def posts_of_following_profiles(request):
        profile = Profile.objects.get(user = request.user)
        users = [user for user in profile.following.all()]
        posts = []
        qs = None
        for u in users:
            p = Profile.objects.get(user=u)
            p_posts = p.user.post_set.all()
        my_posts = profile.profile_posts()
        if len(posts)>0:
            qs = sorted(chain(*posts), reverse=True, key=lambda obj:obj.date_posted)
        paginator = Paginator(qs, 50)
        page = request.GET.get('page')
            posts_list =
        except PageNotAnInteger:
            posts_list =
        except EmptyPage:
            posts_list =
        return render(request,'blog/feeds.html',{'profile':profile,'posts':posts_list})

urlpatterns = [...]
    if settings.DEBUG:
     urlpatterns += static (settings.MEDIA_URL,
                                  document_root = settings.MEDIA_ROOT)

""" Post model """
    class Post(models.Model):
        title = models.CharField(max_length=150)
        content = RichTextField(blank=True, null=True)
        Main_Img = models.ImageField(upload_to='media/images/') 
        date_posted = models.DateTimeField(
        date_updated = models.DateTimeField(auto_now=True)
        author = models.ForeignKey(User, on_delete=models.CASCADE)
        likes = models.ManyToManyField(User, related_name="blogpost", blank=True)
        saves = models.ManyToManyField(User, related_name="blogsave", blank=True)
        def total_likes(self):
            return self.likes.count()
        def total_saves(self):
            return self.saves.count()
        def __str__(self):
            return self.title
        def get_absolute_url(self):
            return reverse('post-detail', kwargs={"pk"})

<div class="m-auto w-100 container">
        <div class="content-section">
            <form method = "post" enctype="multipart/form-data"> 
                {% csrf_token %} 
                {{ form.as_p }} 
                <button type="submit">Upload</button> 

This is the Website i got the code for the Img Upload from:

I have issues to implement them booth.

My problem is to sync the code from the website to my already existing code.

What I have tried:

I tried a lot of YouTube Videos and it doesnt helped me very much, because I am not good at implement the code to my already existing code.
Updated 13-Jul-22 22:34pm

1 solution

You will never get this right just by copying someone else's code and watching YouTube videos. You need to get a good understanding of Django and how it operates. There is a good example of how to handle forms and the associated POST methods at Working with forms | Django documentation | Django[^].
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900