反向' jobpost_detail'参数'()'和关键字参数' {' slug&#39 ;: u' 67itut'}'未找到。尝试过0种模式:[]

时间:2015-05-04 13:32:21

标签: python django django-models django-views mezzanine

我使用django 1.7,python 2.7我安装了搜索模块vacancies空缺创建但在管理面板中维护工作,出现错误。

NoReverseMatch at / ru / admin / careers / jobpost /
Reverse for 'jobpost_detail' with arguments' () 'and keyword arguments' {' slug ': u'67itut'} 'not found. 0 pattern (s) tried: [].

已经所有的pereprobaval

模型

from django.db import models
from django.utils.translation import ugettext_lazy as _

from mezzanine.conf import settings
from mezzanine.core.models import Displayable, RichText, Ownable


class JobPost(Displayable, RichText):
    """
    A career job posting
    """

    class Meta:
        verbose_name = _("Job Post")
        verbose_name_plural = _("Job Posts")
        ordering = ("-publish_date",)

    @models.permalink
    def get_absolute_url(self):
        url_name = "jobpost_detail"
        kwargs = {"slug": self.slug}
        return (url_name, (), kwargs)

    def keyword_list(self):
        return getattr(self, "_keywords", self.keywords.all())

观看次数

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, slug, year=None, month=None, day=None, 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, slug=slug)
    context = {"jobpost": jobpost, "editable_obj": jobpost}
    templates = [u"careers/jobpost_detail_%s.html" % unicode(slug), template]
    return render(request, templates, context)

网址

from django.conf.urls.defaults 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"),


)

Urls项目

from __future__ import unicode_literals

from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin

from mezzanine.core.views import direct_to_template


admin.autodiscover()

# Add the urlpatterns for any custom Django applications here.
# You can also change the ``home`` view to add your own functionality
# to the project's homepage.

urlpatterns = i18n_patterns("",
    # Change the admin prefix here to use an alternate URL for the
    # admin interface, which would be marginally more secure.
    ("^admin/", include(admin.site.urls)),
)

urlpatterns += patterns('',

    # We don't want to presume how your homepage works, so here are a
    # few patterns you can use to set it up.

    # HOMEPAGE AS STATIC TEMPLATE
    # ---------------------------
    # This pattern simply loads the index.html template. It isn't
    # commented out like the others, so it's the default. You only need
    # one homepage pattern, so if you use a different one, comment this
    # one out.

    url("^$", direct_to_template, {"template": "index.html"}, name="home"),

    # HOMEPAGE AS AN EDITABLE PAGE IN THE PAGE TREE
    # ---------------------------------------------
    # This pattern gives us a normal ``Page`` object, so that your
    # homepage can be managed via the page tree in the admin. If you
    # use this pattern, you'll need to create a page in the page tree,
    # and specify its URL (in the Meta Data section) as "/", which
    # is the value used below in the ``{"slug": "/"}`` part.
    # Also note that the normal rule of adding a custom
    # template per page with the template name using the page's slug
    # doesn't apply here, since we can't have a template called
    # "/.html" - so for this case, the template "pages/index.html"
    # should be used if you want to customize the homepage's template.

    # url("^$", "mezzanine.pages.views.page", {"slug": "/"}, name="home"),

    # HOMEPAGE FOR A BLOG-ONLY SITE
    # -----------------------------
    # This pattern points the homepage to the blog post listing page,
    # and is useful for sites that are primarily blogs. If you use this
    # pattern, you'll also need to set BLOG_SLUG = "" in your
    # ``settings.py`` module, and delete the blog page object from the
    # page tree in the admin if it was installed.

    # url("^$", "mezzanine.blog.views.blog_post_list", name="home"),

    # MEZZANINE'S URLS
    # ----------------
    # ADD YOUR OWN URLPATTERNS *ABOVE* THE LINE BELOW.
    # ``mezzanine.urls`` INCLUDES A *CATCH ALL* PATTERN
    # FOR PAGES, SO URLPATTERNS ADDED BELOW ``mezzanine.urls``
    # WILL NEVER BE MATCHED!

    # If you'd like more granular control over the patterns in
    # ``mezzanine.urls``, go right ahead and take the parts you want
    # from it, and use them directly below instead of using
    # ``mezzanine.urls``.
    ("^", include("mezzanine.urls")),


    # MOUNTING MEZZANINE UNDER A PREFIX
    # ---------------------------------
    # You can also mount all of Mezzanine's urlpatterns under a
    # URL prefix if desired. When doing this, you need to define the
    # ``SITE_PREFIX`` setting, which will contain the prefix. Eg:
    # SITE_PREFIX = "my/site/prefix"
    # For convenience, and to avoid repeating the prefix, use the
    # commented out pattern below (commenting out the one above of course)
    # which will make use of the ``SITE_PREFIX`` setting. Make sure to
    # add the import ``from django.conf import settings`` to the top
    # of this file as well.
    # Note that for any of the various homepage patterns above, you'll
    # need to use the ``SITE_PREFIX`` setting as well.

    # ("^%s/" % settings.SITE_PREFIX, include("mezzanine.urls"))

)

# Adds ``STATIC_URL`` to the context of error pages, so that error
# pages can use JS, CSS and images.
handler404 = "mezzanine.core.views.page_not_found"
handler500 = "mezzanine.core.views.server_error"

1 个答案:

答案 0 :(得分:0)

您忘记将应用的url.py添加到根网址。像这样包含careers.urls

urlpatterns += patterns('',
    ...
    url("^$", direct_to_template, {"template": "index.html"}, name="home"),
    ...
    ("^careers/", include("careers.urls")),
    ...
    ("^", include("mezzanine.urls")),
    ...
)