Django如何使用模板标签

时间:2017-12-05 14:31:34

标签: python django

您好我想知道如何拆分字典值字符串

这是我的抓取工具,它返回字典数据

data = {
    {0:'http://..., product name, product price'},
    {1:'http://...2, product name2, product price2'},
    {N:'http://...2, product name2, product price n'}
}

我想用逗号分割这些数据  喜欢,

for value in data.values():
     href, product_name, product_price = str(value).split(",")
Django中的

这是我的crawler.py

import requests
from urllib import parse
from bs4 import BeautifulSoup


def spider(item_name):
    url_item_name = parse.quote(item_name.encode('euc-kr'))

    url = 'http://search.11st.co.kr/SearchPrdAction.tmall?method=getTotalSearchSeller&isGnb=Y&prdType=&category=&cmd=&pageSize=&lCtgrNo=&mCtgrNo=&sCtgrNo=&dCtgrNo=&fromACK=recent&semanticFromGNB=&gnbTag=TO&schFrom=&schFrom=&ID=&ctgrNo=&srCtgrNo=&keyword=&adUrl=&adKwdTrcNo=&adPrdNo=&targetTab=T&kwd=' + url_item_name
    resp = requests.get(url)
    resp.raise_for_status()

    resp.encoding='euc-kr'
    plain_text = resp.text

    soup = BeautifulSoup(plain_text, 'lxml')
    mytag = soup.find_all(True, {"class": ["sale_price", "list_info"]})
    #for link in soup.select('div.list_info p.info_tit a') :
    data = {}
    count = -1;
    for link in mytag:
        if(link.find('a')):
            count+=1
            href = link.find('a').get('href')
            product_name = link.find('a').string
            data[count] = str(href) + ", " + str(product_name)
        else:
            product_price = link.string
            if(product_price):
                data[count] = data[count] +", " + str(product_price)

    for value in data.values():
        print(value)
    resp.close()

    return data

这是我的观点

def post_shop_list(request):
    posts = spider("product name")
    return render(request, 'blog/post_list.html',{'posts' : posts})

这是我的post_list.html

{% for key, value in posts.items %}
    <div>
        <td>{{key}}</td>
        <p>product name :{{value}}</p>
        <h1><a href=href> </a></h1>
        <p>{{ product_price|linebreaksbr}}</p>
    </div>
{% endfor %}

谢谢.. !!

2 个答案:

答案 0 :(得分:2)

创建自定义template filter

from django import template

register = template.Library()

@register.filter(name='split')
def split(value, key):
  """
    Returns the value turned into a list.
  """
  return value.split(key)

在Django模板中,您可以像使用它一样使用它。

# assuming value = "url, product_name, product_price"
# and you have always these three comma separated items in sequence
{% for key, value in posts.items %}
   <tr>
      {% with value|split:"," as details %}
         {% for p in details %}
            <td>{{ p }}</td>
         {% endfor %}
      {% endwith %} 
   </tr>    
{% endfor %}

<强>更新

您还必须在libraries关键字的TEMPLATE列表中输入您的代码文件。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            str(APPS_DIR.path('templates')),
        ],
        'OPTIONS': {

            'loaders': [
                ...
            ],

            'context_processors': [
                ...
            ],
            'libraries':{
               # make your file entry here.
               'filter_tags': 'app.templatetags.filter',
            }
        },
    },
]

然后将此标记加载到要使用split过滤器的html文件的顶部

{% load filter_tags %}

答案 1 :(得分:1)

我建议不要在模板中做这些事情。你最终将一半的视图逻辑嵌入其中。 我建议在您的视图中执行类似的操作或者更好的您的抓取工具

products = []
for key, value in posts.items():
    product = value.split(',')
    # product = [href,name,price]
    product_entry = {
            'key' : key,
            'name' : product[1],
            'href' : product[0],
            'price' : product[2]
    }
    products.append(product_entry)

你最终得到一个很好的数组,把它交给模板,在那里你只需迭代它并读取元素字段。

{% for item in products %}
    <td>{{ item.key }}</td>
    <p>product name :{{ item.name}}</p>
    <h1><a href={{ item.href }}> </a></h1>
    <p>{{ item.price }}</p>
{% endfor %}

或者,按照here

所述创建自定义模板标记