3

I have a python script that extracts links from specified sites and I want to make a single page webapp out of it using Django. This is my first Python and Django project so I want it to be as simple as possible to avoid any fustration but I'm kinda stuck.

this is my views.py

from django.http import HttpResponse
from bs4 import BeautifulSoup
import urllib2, sys
import urlparse
import re
import time, threading

def businessghana(request):
    site = "http://www.businessghana.com/portal/jobs"
    hdr = {'User-Agent' : 'Mozilla/5.0'}
    req = urllib2.Request(site, headers=hdr)
    jobpass = urllib2.urlopen(req)
    soup = BeautifulSoup(jobpass)
    for tag in soup.find_all('a', href = True):
         tag['href'] = urlparse.urljoin('http://www.businessghana.com/portal/', tag['href'])
    return map(str, soup.find_all('a', href = re.compile('.getJobInfo')))
    threading.Timer(3600, businessghana).start()

There are about 10 of these functions for 10 different sites but they are of similar nature so for the Django project I'm using only 1 one of them. Again, to keep it simple and avoid fustration

this is my urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^hello/$', 'listing.views.businessghana'),

    url(r'^admin/', include(admin.site.urls)),
)

this is my models.py:

(I think I'm spewing nonsense but this is the best i could come up with)

class jobLinks(models.Model):
    links = models.CharField(max_length = 200)
    pub_date = models.DateTimeField('date retrieved')

    def __unicode__(self):
        return self.links

When I visit the url:

127.0.0.1:8000/hello/

I get the error 'list' object has no attribute 'status_code'.

This is suppose to be a single page site that simply displays the links that were retrieved from the functions in the views. I have other plans to make it a bit more complex but I want to start with this.

Please what am I missing?

Is the models.py content making any sense?

Thanks in advance

4
  • 3
    You haven't referenced the model anywhere, so it makes no difference whether it makes any sense or not. (The threading.Timer call, on the other hand, makes absolutely no sense at all.) Commented Dec 19, 2013 at 17:56
  • i was hoping the threading.Timer call will let the function run at the specified interval. Is there a better way to achieve that behaviour, I tried it in IDLE and it was functioning as expected though Commented Dec 19, 2013 at 18:19
  • 1
    Well assuming that it does call the function again, what are you expecting to happen with the return values? Where would they go? Commented Dec 19, 2013 at 18:42
  • the page is periodically updated so the new links will replace the old links.for the entire codebase for the app, I have it such that the new script will only check for the new links and append it to the old links. But since i want the barebone for the django version to understand it, that's why I chose only one of the function and call it periodically.. Nonetheless, if it's still nuts I will welcome a more sane approach. I'm jx learning programming for the first time so I prefer to take up best practices from the start. Thanks Commented Dec 19, 2013 at 20:02

2 Answers 2

3

A Django view must return an HttpResponse, not a list.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. But how do I pass the list as an HttpResponse
1

Like @bruno said Django views must return HttpResponse.

To do that I simply had to wrap my return statement around an HttpResponse so it looked like this:

return HttpResponse(map(str, soup.find_all('a', href = re.compile('.getJobInfo'))))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.