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 >
<div >
<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: bit.ly/3IAKNQK
I have issues to implement them booth.
My problem is to sync the code from the website to my already existing code.
CodePudding user response:
from django import forms # <----------------------- module 'django.forms'
from django.forms import fields
from .models import Post, Comment
class Post(forms.Post): # <----------------------- has no attribute 'Post'
# ...
AttributeError: module 'django.forms' has no attribute 'Post'
The error message is quite self explanatory: you're trying to inherit from a class named Post
from django.forms
, but there's no class Post
in django.forms
.
CodePudding user response:
The problem is that there is no class named Post
in django.forms
module.
I am not sure what example you are refering to as https://de.acervolima.com/python-bilder-in-django-hochladen returns 404 not found, but I guess you replaced some class name in the example.
In Django, file upload can be implemented using FileField
in django.forms
These Docs might help.
https://docs.djangoproject.com/en/4.0/topics/http/file-uploads/ https://docs.djangoproject.com/en/4.0/ref/forms/api/#binding-uploaded-files