共享 Post 不会 Post 图像到后端的共享对象
Shared Post Doesn't Post Images to the Shared Object In the backend
我很难用我的功能分享 post,我的挑战是每当我分享 post 时,原始 post 中的分享图片不会 post 图像到新的 post 图像字段,而是重新 post 原始共享 post 字段中的图像,因此无法显示共享图像!我的问题是为什么我在原始图像字段而不是新的共享 post 字段中得到 post 被重新 post 的效果!希望得到解决方案?
Here is my function for sharing post:
def share_post(request, pk):
original_post = Post.objects.prefetch_related('groupimage_set').get(pk=pk)
form = ShareForm(request.POST, request.FILES)
if form.is_valid():
new_post = Post(
shared_body = request.POST.get('description'),
description = original_post.description,
username = original_post.username,
date_posted = original_post.date_posted,
group = original_post.group,
video = original_post.video,
shared_on = datetime.now(),
shared_user = request.user)
new_post.save()
form = GroupImageForm(request.POST, request.FILES)
if form.is_valid():
new_post = form.save(commit=False)
for image in original_post.groupimage_set.all():
new_post = GroupImage(post=original_post,
group=original_post.group,
shared_user=request.user,
image=image.image)
new_post.save()
return redirect('group:main',original_post.group.pk)
else:
form = GroupImageForm(request.POST, request.FILES)
ctx = {'form':form, 'original_post':original_post}
return render(request,'group/share_form.html', ctx)
这是我的图像模型 post
class Post(models.Model):
group = models.ForeignKey(GroupEx, on_delete=models.CASCADE, related_name="group_post")
username = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE ,related_name="group_user")
description = models.TextField(max_length=500, blank=True)
video = models.FileField(upload_to="group_videos", blank=True)
shared_body = models.TextField(blank=True, null=True)
date_posted = models.DateTimeField(auto_now_add=True)
shared_on = models.DateTimeField(blank=True, null=True)
shared_user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True, related_name='+')
群像模型
class GroupImage(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
group = models.ForeignKey(GroupEx,on_delete=models.SET_NULL, null=True)
shared_user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True, related_name='+')
image = models.ImageField(upload_to='GroupImageLibrary/', blank=True, null=True)
错误是您将 original_post
传递给 GroupImage model
而不是 new_post
。
尝试更改此部分
if form.is_valid():
new_post = Post(
shared_body = request.POST.get('description'),
description = original_post.description,
username = original_post.username,
date_posted = original_post.date_posted,
group = original_post.group,
video = original_post.video,
shared_on = datetime.now(),
shared_user = request.user)
new_post.save()
form = GroupImageForm(request.POST, request.FILES)
if form.is_valid():
new_post = form.save(commit=False)
for image in original_post.groupimage_set.all():
new_post = GroupImage(post=original_post,
group=original_post.group,
shared_user=request.user,
image=image.image)
new_post.save()
到
if form.is_valid():
new_post1 = Post(
shared_body = request.POST.get('description'),
description = original_post.description,
username = original_post.username,
date_posted = original_post.date_posted,
group = original_post.group,
video = original_post.video,
shared_on = datetime.now(),
shared_user = request.user)
new_post1.save()
for image in original_post.groupimage_set.all():
new_post = GroupImage(post=new_post1,
group=original_post.group,
shared_user=request.user,
image=image.image)
new_post.save()
print("new_post",new_post) # just for debugging the program...
我认为处理共享的另一种方法 post 是在 post 上设置外键 itself.let 我知道您是否需要更多帮助。
我很难用我的功能分享 post,我的挑战是每当我分享 post 时,原始 post 中的分享图片不会 post 图像到新的 post 图像字段,而是重新 post 原始共享 post 字段中的图像,因此无法显示共享图像!我的问题是为什么我在原始图像字段而不是新的共享 post 字段中得到 post 被重新 post 的效果!希望得到解决方案?
Here is my function for sharing post:
def share_post(request, pk):
original_post = Post.objects.prefetch_related('groupimage_set').get(pk=pk)
form = ShareForm(request.POST, request.FILES)
if form.is_valid():
new_post = Post(
shared_body = request.POST.get('description'),
description = original_post.description,
username = original_post.username,
date_posted = original_post.date_posted,
group = original_post.group,
video = original_post.video,
shared_on = datetime.now(),
shared_user = request.user)
new_post.save()
form = GroupImageForm(request.POST, request.FILES)
if form.is_valid():
new_post = form.save(commit=False)
for image in original_post.groupimage_set.all():
new_post = GroupImage(post=original_post,
group=original_post.group,
shared_user=request.user,
image=image.image)
new_post.save()
return redirect('group:main',original_post.group.pk)
else:
form = GroupImageForm(request.POST, request.FILES)
ctx = {'form':form, 'original_post':original_post}
return render(request,'group/share_form.html', ctx)
这是我的图像模型 post
class Post(models.Model):
group = models.ForeignKey(GroupEx, on_delete=models.CASCADE, related_name="group_post")
username = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE ,related_name="group_user")
description = models.TextField(max_length=500, blank=True)
video = models.FileField(upload_to="group_videos", blank=True)
shared_body = models.TextField(blank=True, null=True)
date_posted = models.DateTimeField(auto_now_add=True)
shared_on = models.DateTimeField(blank=True, null=True)
shared_user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True, related_name='+')
群像模型
class GroupImage(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
group = models.ForeignKey(GroupEx,on_delete=models.SET_NULL, null=True)
shared_user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True, related_name='+')
image = models.ImageField(upload_to='GroupImageLibrary/', blank=True, null=True)
错误是您将 original_post
传递给 GroupImage model
而不是 new_post
。
尝试更改此部分
if form.is_valid():
new_post = Post(
shared_body = request.POST.get('description'),
description = original_post.description,
username = original_post.username,
date_posted = original_post.date_posted,
group = original_post.group,
video = original_post.video,
shared_on = datetime.now(),
shared_user = request.user)
new_post.save()
form = GroupImageForm(request.POST, request.FILES)
if form.is_valid():
new_post = form.save(commit=False)
for image in original_post.groupimage_set.all():
new_post = GroupImage(post=original_post,
group=original_post.group,
shared_user=request.user,
image=image.image)
new_post.save()
到
if form.is_valid():
new_post1 = Post(
shared_body = request.POST.get('description'),
description = original_post.description,
username = original_post.username,
date_posted = original_post.date_posted,
group = original_post.group,
video = original_post.video,
shared_on = datetime.now(),
shared_user = request.user)
new_post1.save()
for image in original_post.groupimage_set.all():
new_post = GroupImage(post=new_post1,
group=original_post.group,
shared_user=request.user,
image=image.image)
new_post.save()
print("new_post",new_post) # just for debugging the program...
我认为处理共享的另一种方法 post 是在 post 上设置外键 itself.let 我知道您是否需要更多帮助。