0

Error is occuring from media.py, it looks like my url is none. but I don't understand why. how do I set url equals to the posted url from a user? If you see my views.py, I tried to set it like that. url equals to self.request.GET.get('url'). Did I do it wrong?

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)

1 Answers1

1

In views.py do like:

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', False)
    if url:
        image = extract(url)
    self.object.save()
    return HttpResponseRedirect(reverse('post', args=[self.object.slug]))
Anush Devendra
  • 5,285
  • 1
  • 32
  • 24
  • Hello, thank you that made the error dissapeared but the image won't be shown up,,, oh shoot maybe I set the whole thing up. the image was suppose to be shown up in index.html next to the title.... this is driving me crazy –  Dec 26 '15 at 19:13
  • like I have described in the question, if I were to extract image from the user posted url, is it correct to put url = self.request.GET.get('url', False) if url: image = extract(url) inside index view or add_post view? the image that's being extracted will be displayed in index.html but the data is extracted from add_post.html –  Dec 26 '15 at 19:23
  • @haloyoba keep it in the add_post view. Once you extract the `url` you need to save the url in the post model/Table. Next you can display the image in the index.html using the url from the `post` object. – Anush Devendra Dec 26 '15 at 19:32
  • that's what I thought at first, just got confused. btw, I'm extracting the image not the url. still same thing right? I'm confused again, how do I save and display the image, now that it's extracted(if it's done properly)/(is there anyway I can check if it's done properly)? –  Dec 26 '15 at 19:39
  • @haloyoba you don't need to extract the image to save it. Just google `django save/upload image` i'm sure you'll find a blog/post explaining it throughly...you can start here: http://stackoverflow.com/questions/16381241/django-save-image-from-url-and-connect-with-imagefield – Anush Devendra Dec 26 '15 at 19:49
  • maybe I didn't put it into right words. This is what I'm trying to do. Let user post url, the url is submitted and extract main image from the link user posted. Display that image. I;m at a point the image got extracted as the user posted link(thank you for fixing the error) now I'm simply trying to extract the image –  Dec 26 '15 at 19:54
  • ***trying to display the image/not extract –  Dec 26 '15 at 20:00
  • @haloyoba ok...then please ask this as a separate question, this cannot be explained in the comments. – Anush Devendra Dec 26 '15 at 20:08
  • Hello, I just uploaded a new question. Thank you,http://stackoverflow.com/questions/34474910/trying-to-display-the-image-that-got-extracted-from-the-user-posted-link –  Dec 26 '15 at 20:19
  • http://stackoverflow.com/questions/34474910/trying-to-display-the-image-that-got-extracted-from-the-user-posted-link I made a new thread just like you told me :) –  Dec 26 '15 at 20:26