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
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
urlpatterns = [...]
if settings.DEBUG:
urlpatterns += static (settings.MEDIA_URL,
document_root = settings.MEDIA_ROOT)
models.py
""" 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
<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.