2

I am using below code to add meta keywords -

in view.py

@template_render("mysite/category.html")
def category(request, slug):
    slug = slug.lower()
    product_type = local_settings.CATEGORY_NAME_TO_ID.get(slug, False)
    if not product_type:
        raise Http404
    products = models.Product.objects.active().filter(product_type = product_type).all()
    return { 'products' : products, 'slug' : slug, 'key':'wholesale ipad, ipad with retina display, ipad mini, ipad 3, ipad 2',}

And in template file -

{% extends "base.html"%}
{%load appletrade_tags%}
{% block key %}siteTrade - {{key}}{% endblock %}
{%block title%}site Trade - {{slug}}{%endblock%}

But it's not reflecting. I have checked in view source there is no keyword.

But Yes,title is reflecting.

Can you please help me to find out where I am wrong ?

EDIT :

base.html

{% extends "base.html"%}
{% block key %}{%if page.key%}{{page.key}}{%else%}{{block.super}}{%endif%}{% endblock %}
{% block desc %}{%if page.desc%}{{page.desc}}{%else%}{{block.super}}{%endif%}{% endblock %}
{%block title%}{%if page.title%}{{page.title}}{%else%}{{block.super}}{%endif%}{%endblock%}
{%block content%}
{%endblock%}
4

2 回答 2

2

这是一种为您的 django 站点自动执行关键字的方法。反正我不喜欢手动输入东西。

这是一个读取模板文件并计算主要单词的函数,并返回页面中使用的单词列表。

# meta_gen.py
# create meta keywords for webpage automagically
# MIT License
# Author: Daniel P. Clark 6ftdan@gmail.com
import collections, string, StringIO, re
from django.utils.html import strip_tags

# Counts words in template files and insert keywords into page
word_count_min = 2
word_length_min = 4

nogo_list = [] # Optional list of words you may not want as meta tags.

# meta_keywords ( html string ) => 
#   returns non html keyword list, as a comma seperated string,
#   for words fitting qualifications as chosen above.
def meta_keywords(str_file):
    c = collections.Counter()
    strIO_Obj = StringIO.StringIO(str_file)
    for line in strIO_Obj.readlines():
        c.update(re.findall(r"[\w']+", strip_tags(line).lower()))
    wordlist = []
    for word, count in c.most_common():
        if len(word) > (word_length_min-1) and count > (word_count_min-1) and word not in nogo_list: wordlist.append(word)
    return string.join(wordlist, ',')

将 meta_gen.py 放在您的主项目文件夹中。然后将这些部分添加到您的每个 views.py 文件中。

# views.py
from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from project.meta_gen import meta_keywords

this_template = "apptemplate.html"

def tabs(request):
    return render_to_response(this_template, { 'title' : "Page Name", 'keys' : meta_keywords(render_to_string(this_template)) })

最后,在您的主模板 base.html 中放置关键字的元标记。

# base.html
<head>
<title>Site Name - {{ title }}</title>
<meta name="keywords" content="{{ keys }}" />
</head>

就是这样。继承基本模板并具有views.py 代码的所有页面都将插入关键字元标记,其中包含在您的页面上重复的单词。

我意识到这可以改进和优化。速度对我来说不是问题。所以欢迎输入。

于 2014-01-17T02:14:31.180 回答
2

您需要使用 render 或 render_to_response 将上下文传递给模板。slug 对象是否出现在页面上?

from django.shortcuts import render_to_response

def category(request, slug):
    slug = slug.lower()
    product_type = local_settings.CATEGORY_NAME_TO_ID.get(slug, False)
    if not product_type:
        raise Http404
    products = models.Product.objects.active().filter(product_type = product_type)
    context = {
        'slug': slug,
        'products': products,
        'key': 'wholesale ipad, ipad with retina display, ipad mini, ipad 3, ipad 2',
    }
    return render_to_response('appletrade/category.html', context, context_instance=RequestContext(request))
于 2013-07-30T10:28:57.153 回答