【问题标题】:TypeError at /careers/test/ jobpost_detail() got an unexpected keyword argument 'slug'/careers/test/jobpost_detail() 的 TypeError 有一个意外的关键字参数“slug”
【发布时间】:2015-05-10 11:25:54
【问题描述】:

我安装了应用程序职业夹层并在创建测试位置后翻页查看错误TypeError at /careers/test/ jobpost_detail () 得到了一个意外的关键字参数“slug”。我该如何解决这个问题?

观看次数

from calendar import month_name
from django.shortcuts import get_object_or_404
from collections import defaultdict
from django.contrib.contenttypes.models import ContentType
from django import VERSION

from careers.models import JobPost
from mezzanine.conf import settings
from mezzanine.generic.models import AssignedKeyword, Keyword
from mezzanine.utils.views import render, paginate


def jobpost_list(request, tag=None, year=None, month=None, template="careers/jobpost_list.html"):
    """
    Display a list of job posts that are filtered by year, month.
    """
    settings.use_editable()
    templates = []
    jobposts = JobPost.objects.published()
    if tag is not None:
        tag = get_object_or_404(Keyword, slug=tag)
        jobposts = jobposts.filter(keywords__in=tag.assignments.all())
    if year is not None:
        jobposts = jobposts.filter(publish_date__year=year)
        if month is not None:
            jobposts = jobposts.filter(publish_date__month=month)
            month = month_name[int(month)]
    # We want to iterate keywords and categories for each blog post
    # without triggering "num posts x 2" queries.
    #
    # For Django 1.3 we create dicts mapping blog post IDs to lists of
    # categories and keywords, and assign these to attributes on each
    # blog post. The Blog model then uses accessor methods to retrieve
    # these attributes when assigned, which will fall back to the real
    # related managers for Django 1.4 and higher, which will already
    # have their data retrieved via prefetch_related.

    jobposts = jobposts.select_related("user")
    if VERSION >= (1, 4):
        jobposts = jobposts.prefetch_related("keywords__keyword")
    else:
        if jobposts:
            ids = ",".join([str(p.id) for p in jobposts])
        keywords = defaultdict(list)
        jobpost_type = ContentType.objects.get(app_label="careers", model="jobpost")
        assigned = AssignedKeyword.objects.filter(jobpost__in=jobposts, content_type=jobpost_type).select_related("keyword")
        for a in assigned:
            keywords[a.object_pk].append(a.keyword)
        for i, post in enumerate(jobposts):
            setattr(jobposts[i], "_keywords", keywords[post.id])
    jobposts = paginate(jobposts, request.GET.get("page", 1),
                          settings.CAREERS_PER_PAGE,
                          settings.MAX_PAGING_LINKS)
    context = {"jobposts": jobposts, "year": year, "month": month, "tag": tag}
    templates.append(template)
    return render(request, templates, context)


def jobpost_detail(request, template="careers/jobpost_detail.html"):
    """. Custom templates are checked for using the name
    ``careers/jobpost_detail_XXX.html`` where ``XXX`` is the job
    posts's slug.
    """
    jobposts = JobPost.objects.published()
    jobpost = get_object_or_404(jobposts)
    context = {"jobpost": jobpost, "editable_obj": jobpost}
    templates = [u"careers/jobpost_detail_%s.html" %(slug), template]
    return render(request, templates, context)

html

{% extends "careers/jobpost_list.html" %}
{% load mezzanine_tags keyword_tags i18n %}

{% block meta_title %}{{ jobpost.meta_title }}{% endblock %}

{% block meta_keywords %}{% metablock %}
{% keywords_for jobpost as tags %}
{% for tag in tags %}{% if not forloop.first %}, {% endif %}{{ tag }}{% endfor %}
{% endmetablock %}{% endblock %}

{% block meta_description %}{% metablock %}
{{ jobpost.description }}
{% endmetablock %}{% endblock %}

{% block title %}
{% editable jobpost.title %}{{ jobpost.title }}{% endeditable %}
{% endblock %}

{% block breadcrumb_menu %}
{{ block.super }}
<li class="active">{{ jobpost.title }}</li>
{% endblock %}

{% block main %}

<h6>
    {% trans "Posted" %} {{ jobpost.publish_date|timesince }} {% trans "ago" %}.
</h6>

{% editable jobpost.content %}
{{ jobpost.content|richtext_filter|safe }}
{% endeditable %}

{% keywords_for jobpost as tags %}
{% if tags %}
{% spaceless %}
<ul class="unstyled tags">
    <li>{% trans "Tags" %}:</li>
    {% for tag in tags %}
    <li><a href="{% url "jobpost_list_tag" tag.slug %}">{{ tag }}</a></li>
    {% endfor %}
</ul>
{% endspaceless %}
{% endif %}


{% set_short_url_for jobpost %}
<a class="btn small primary share-twitter" target="_blank" href="http://twitter.com/home?status={{ jobpost.short_url|urlencode }}%20{{ jobpost.title|urlencode }}">{% trans "Share on Twitter" %}</a>
<a class="btn small primary share-facebook" target="_blank" href="http://facebook.com/sharer.php?u={{ request.build_absolute_uri }}&amp;t={{ jobpost.title|urlencode }}">{% trans "Share on Facebook" %}</a>

{% endblock %}

网址

from django.conf.urls import patterns, url

# Job Post patterns.
urlpatterns = patterns("careers.views",

    url("^tag/(?P<tag>.*)/$",
        "jobpost_list",
        name="jobpost_list_tag"),

    url("^archive/(?P<year>\d{4})/(?P<month>\d{1,2})/$",
        "jobpost_list",
        name="jobpost_list_month"),

    url("^archive/(?P<year>.*)/$",
        "jobpost_list",
        name="jobpost_list_year"),

    url("^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>.*)/$",
        "jobpost_detail",
        name="jobpost_detail_date"),

    url("^(?P<slug>.*)/$",
        "jobpost_detail",
        name="jobpost_detail"),

    url("^$",
        "jobpost_list",
        name="jobpost_list"),
)

【问题讨论】:

    标签: python django django-templates django-views


    【解决方案1】:

    错误告诉你到底发生了什么:你的“jobpost_detail” URL 捕获了一个slug 参数并将其传递给视图,但该视图不需要一个 slug,只有请求和一个模板。此外,在这种情况下,您并没有做任何事情来获得由 slug 识别的实际帖子:您总是得到第一个发布的帖子。

    我怀疑您想要执行以下操作:

    def jobpost_detail(request, slug, template="careers/jobpost_detail.html"):
        jobposts = JobPost.objects.published()
        jobpost = get_object_or_404(jobposts, slug=slug)
    

    【讨论】:

    • 现在 / Career / Test / 中出现错误 TemplateSyntaxError / 无法解析其余部分:'''jobpost_list_tag from '''jobpost_list_tag
    • 嗯,这似乎是您尚未发布的模板中的错误。
    • Roserman 显示有错误
    • {{ tag }}
  • 我想通了,但现在他在 /careers/test/ global name 'Career' is not defined 处写 NameError
  • 再一次,在一些你没有发布的代码中,你在没有导入的情况下引用了职业模型。
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签