这就是我想要做的。让用户发布网址,提交网址并从发布的链接用户中提取主图像。显示该图像。我;在某一点上,当用户发布链接时,图像被提取出来(感谢您修复错误),现在我只是想显示图像
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']
答案 0 :(得分:1)
我没有看到你的models.py文件。所以我会这样做:
请确保您的课程中的models.py中有“列”,例如
img = models.FileField(upload_to ='static / your_directory /',null = True)
没有“null = True”Django无法进行迁移。
提交后,Django会将该图片放入上述目录。
设置上下文数据后在模板中渲染图像:
< img src =“/ {{ your_object .img}}”> !别忘了SLASH !!!
这是一个没有AJAX的设置。请阅读下面的评论,以获取更多信息,thx。