Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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添加meta关键字_Python_Django_Keyword_Meta Tags - Fatal编程技术网

Python 如何使用django添加meta关键字

Python 如何使用django添加meta关键字,python,django,keyword,meta-tags,Python,Django,Keyword,Meta Tags,我使用下面的代码添加元关键字- 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 product

我使用下面的代码添加元关键字-

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',}
和模板文件中的-

{% extends "base.html"%}
{%load appletrade_tags%}
{% block key %}siteTrade - {{key}}{% endblock %}
{%block title%}site Trade - {{slug}}{%endblock%}
但它没有反映。我已签入视图源,但没有关键字

但是,是的,标题是反映

你能帮我找出哪里错了吗

编辑:

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%}

您需要使用render或render_to_响应将上下文传递给模板。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))

您需要使用render或render_to_响应将上下文传递给模板。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))

这里有一种方法可以为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>
#base.html
站点名称-{{title}
就这样。所有继承基本模板并具有views.py代码的页面都将插入关键字元标记,其中包含在页面上重复的单词


我意识到这是可以改进和优化的。我不在乎速度。因此,欢迎输入。

这里有一种方法可以为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>
#base.html
站点名称-{{title}
就这样。所有继承基本模板并具有views.py代码的页面都将插入关键字元标记,其中包含在页面上重复的单词


我意识到这是可以改进和优化的。我不在乎速度。因此,欢迎输入。

谢谢。我是python新手。请突出显示我必须在代码中对其进行更改的位置好吗?已尝试,但出现错误“view appletrade.views.category未返回HttpResponse对象”。@HelloPython是否导入了render\u to\u响应?应该还有更详细的追踪。他忘了更改他的返回声明,他还在返回那份声明。好的,我能理解。谢谢:)谢谢。我是python新手。请突出显示我必须在代码中对其进行更改的位置好吗?已尝试,但出现错误“view appletrade.views.category未返回HttpResponse对象”。@HelloPython是否导入了render\u to\u响应?应该还有更详细的追踪。他忘了更改他的返回声明,他还在返回那份声明。好的,我能理解。谢谢:)真是太棒了,我正在寻找一种动态的方式来生成我的关键字,这个想法似乎非常完美,我也将使用它来从我的模型生成关键字!真是太棒了,我正在寻找一种动态的方式来生成我的关键字,这个想法似乎非常完美,我也将使用它来从我的模型生成关键字!