Click here to Skip to main content
15,881,172 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:
forms.py

Python
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']

posts.py

    """ Home page with all posts """
    def first(request):
        context = {
            'posts':Post.objects.all()
        }
        return render(request, 'blog/first.html', context)
    
    """ Posts of following user profiles """
    @login_required
    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()
            posts.append(p_posts)
        my_posts = profile.profile_posts()
        posts.append(my_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')
        try:
            posts_list = paginator.page(page)
        except PageNotAnInteger:
            posts_list = paginator.page(1)
        except EmptyPage:
            posts_list = paginator.page(paginator.num_pages)
      
        return render(request,'blog/feeds.html',{'profile':profile,'posts':posts_list})



urls.py

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


models.py

Python
""" 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(default=timezone.now)
        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":self.pk})


post_form.py

Python
<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> 
            </form>
        </div>
        </div>


This is the Website i got the code for the Img Upload from:
https://de.acervolima.com/python-bilder-in-django-hochladen

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.
Posted
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