Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django中使用列表中的信息填充html表_Python_Django_Django Models_Django Forms_Django Views - Fatal编程技术网

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

Python 如何在django中使用列表中的信息填充html表,python,django,django-models,django-forms,django-views,Python,Django,Django Models,Django Forms,Django Views,我想在django应用程序中,用urlparse.py的结果填充base.html中的表(这将返回一个站点中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

我想在django应用程序中,用urlparse.py的结果填充
base.html
中的表(这将返回一个站点中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]
url.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>

等级
网站
描述
某物
{{site.url}}
某物

有人能给我指出正确的方向吗?如何将urlparse.py的结果解析到第二个
标记中,以及在其他文件中会出现什么修改?(表单、视图、URL)。

将URL列表传递给模板,并使用标记循环它们

url.py

urlpatterns = patterns('',
    url(r'^$', 'myapp.views.top_urls', name='home'),
    url(r'^admin/', include(admin.site.urls)),
)
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})
视图.py

urlpatterns = patterns('',
    url(r'^$', 'myapp.views.top_urls', name='home'),
    url(r'^admin/', include(admin.site.urls)),
)
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\u url.html

...
<tbody>
    {% for url in urls %}
        <tr>
            <td>Something</td>
            <td>{{ url }}</td>
            <td>something</td>
        </tr>
    {% endfor %}
</tbody>
...
。。。
{url%中的url为%1}
某物
{{url}}
某物
{%endfor%}
...

感谢您的快速回答。不幸的是,似乎什么也没有发生。桌子还是空的。有什么提示吗?你说的“桌子还是空的”是什么意思?表在
中没有行?或者带有url的
为空?我的意思是我的表中没有填充的行。它只是说:
表中没有可用的数据
。因此,URL并没有像应该的那样解析到表中。添加
打印URL
,就像我更新的答案一样,并检查devsever控制台的输出。我怀疑
p.site\u列表
是空的。我清除了缓存,出现了另一个问题:
全局名称“MyHTMLPasser”未定义