如何使用django中列表中的信息填充html表

时间:2015-03-30 00:08:50

标签: python django django-models django-forms django-views

我想在我的django应用程序中使用来自base.html的表格来填充urlparse.py的结果(这将返回来自站点的20个URL的列表)。

models.py

from django.db import models
from django.utils.encoding import smart_unicode

# Create your models here.

urlparse.py

import HTMLParser, urllib2

class MyHTMLParser(HTMLParser.HTMLParser):
    site_list = []

    def reset(self):
        HTMLParser.HTMLParser.reset(self)
        self.in_a = False
        self.next_link_text_pair = None
    def handle_starttag(self, tag, attrs):
        if tag=='a':
            for name, value in attrs:
                if name=='href':
                    self.next_link_text_pair = [value, '']
                    self.in_a = True
                    break
    def handle_data(self, data):
        if self.in_a: self.next_link_text_pair[1] += data
    def handle_endtag(self, tag):
        if tag=='a':
            if self.next_link_text_pair is not None:
                if self.next_link_text_pair[0].startswith('/siteinfo/'):
                    self.site_list.append(self.next_link_text_pair[1])
            self.next_link_text_pair = None
            self.in_a = False

if __name__=='__main__':
    p = MyHTMLParser()
    p.feed(urllib2.urlopen('http://www.alexa.com/topsites/global').read())
    print p.site_list[:20]

urls.py

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    #url(r'^$', 'signups.views.home', name='home'),
    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

views.py

from django.shortcuts import render, render_to_response, RequestContext

# Create your views here.

base.html文件

<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Rank</th>
                <th>Website</th>
                <th>Description</th>
            </tr>
        </thead>

        <tbody>
            <tr>
                <td>Something</td>
                <td>{{site.urls}}</td><!--What to put here ?-->
                <td>something</td>
            </tr>
        </tbody>
    </table>

有人能指出我正确的方向吗?如何将urlparse.py中的结果解析为第二个<td>标记,以及其他文件中会出现哪些修改? (表单,视图网址)。

1 个答案:

答案 0 :(得分:3)

将网址列表传递给模板,然后使用{% for %}标记循环播放。

<强> urls.py

urlpatterns = patterns('',
    url(r'^$', 'myapp.views.top_urls', name='home'),
    url(r'^admin/', include(admin.site.urls)),
)

<强> views.py

def top_urls(request):
    p = MyHTMLParser()
    p.feed(urllib2.urlopen('http://www.alexa.com/topsites/global').read())
    urls = p.site_list[:20]
    print urls
    return render(request, 'top_urls.html', {'urls': urls})

<强> top_urls.html

...
<tbody>
    {% for url in urls %}
        <tr>
            <td>Something</td>
            <td>{{ url }}</td>
            <td>something</td>
        </tr>
    {% endfor %}
</tbody>
...