gpt4 book ai didi

python - 尝试显示从用户发布的链接中提取的图像

转载 作者:太空宇宙 更新时间:2023-11-03 17:07:13 24 4
gpt4 key购买 nike

这就是我正在努力做的事情。让用户发布 url,提交 url 并从用户发布的链接中提取主图像。显示该图像。我;在用户发布链接时图像被提取(感谢您修复错误),现在我只是尝试显示图像

Media.py
import json import urllib2 from urllib2 import Request from goose import Goose
def get_content(url):
"""This function is intended to return content from url.
:param url: URL to get content
:return: The response from url
"""
req = urllib2.Request(url, headers={'User-Agent': "Magic Browser"})
response = urllib2.Request.urlopen(req).read()
return response


def extract(url):
url = get_content(url)
g = Goose()
article = g.extract(url=url)
resposne = {'image':article.top_image.src}
return json.dumps(resposne)

views.py



class PostCreateView(CreateView):

model = Post
form_class = PostForm
template_name = 'main/add_post.html'

def form_valid(self, form):
self.object = form.save(commit=False)
# any manual settings go here
self.object.moderator = self.request.user
url = self.request.GET.get('url')
image = extract(url)
self.object.save()
return HttpResponseRedirect(reverse('post', args=[self.object.slug]))

@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):

return super(PostCreateView, self).dispatch(request, *args, **kwargs)

def index(request):
categories = Category.objects.all()

try:
sort = request.GET["sort"].strip()
sort_method = SortMethods[sort]
page = request.GET["page"].strip()
except KeyError:
sort_method = SortMethods.score
page = 1

if sort_method == SortMethods.date:
thread_list = Post.objects.order_by("-pub_date")
else:
thread_list = Post.objects.all()
thread_list = sorted(thread_list, key=lambda x: x.get_score(), reverse=True)

paginator = Paginator(thread_list, 30)

try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)

context = {
"posts": posts,
"pages": paginator.page_range,
"sort": sort_method.name,
"categories":categories,

}
return render(request, "main/index.html", context)


Models.py

class Post(models.Model):
category = models.ForeignKey(Category)
pub_date = models.DateTimeField(auto_now_add = True)
title = models.CharField(max_length = 100)
content = FroalaField()
url = models.URLField(max_length=250, blank=True, null=True)
moderator = models.ForeignKey(User)
rank_score = models.FloatField(default= 1)

views = models.IntegerField(default=0)
image = models.ImageField(upload_to="images",blank=True, null=True)
slug = models.CharField(max_length=100, unique=True)

objects = models.Manager()


forms.py

class PostForm(forms.ModelForm):
title = forms.CharField(max_length=128, help_text="plz enter")

url = forms.URLField(max_length=200,
help_text="Please enter the URL of the page.", required=False)
views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
content = forms.CharField(widget=FroalaEditor)
class Meta:
model = Post

exclude = ['pub_date', 'moderator', 'rank_score', 'image','slug']

最佳答案

我没有看到您的 models.py 文件。所以我会这样做:

  1. 确保您的类(class)中的 models.py 中有一个“列”,例如

    img = models.FileField(upload_to='static/your_directory/', null=True)

    如果没有“null=True”,Django 无法进行迁移。

  2. 提交后,Django 会将该图像放入提到的目录中。

  3. 设置上下文数据后在模板中渲染图像:

    !!!不要忘记斜杠!!!

这是一个没有 AJAX 的设​​置。请阅读下面的评论以获取更多信息,谢谢。

关于python - 尝试显示从用户发布的链接中提取的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34474910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com