This is an example of a structure, also shows how to use the 
get_absolute_url method. Note that the first parameter that is passed to 
the same method (city_detail) is the name assigned to the url in 
urlpatterns, and that the third parameter is a list in which is slug, which 
matches the expected parameter in the urlpattern, and also expected by the 
view. Note also that the view specifies the template to be used.

# in the models.py file

class City(Displayable):

    # fields, class Meta, etc.

    @models.permalink
    def get_absolute_url(self):
        return ("city_detail", (), {"slug": self.slug})
    

# in the views.py file

def city_detail(request, slug, template="cities/city_detail.html", 
extra_context=None):
    """
    Custom templates are checked for using the name
    ``cities/city_detail_XXX.html`` where ``XXX`` is the city slug.
    """
    city = get_object_or_404(City, slug=slug, status=2)
    context = {"city": city,
               "editable_obj": city}
    context.update(extra_context or {})
    templates = [u"cities/city_detail_%s.html" % str(slug), template]
    return TemplateResponse(request, templates, context)


# in the urls.py file

urlpatterns = [
    url("^(?P<slug>.*)%s$" % _slash, city_detail, name="city_detail"),
]


# in the templates/cities/city_detail.html file

{% extends "base.html" %}
{% load mezzanine_tags keyword_tags %}

{% block meta_title %}{{ city.meta_title }}{% endblock %}

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

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

{% block title %}
    {{ city.title }}
{% endblock %}

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

{% block main %}
    {{ city.content }}
{% endblock %}



Il giorno domenica 22 ottobre 2017 00:36:31 UTC+2, Tom Tanner ha scritto:
>
>
> Hey all,
>
> I've started a new Mezzanine site. I'm trying to make a page where I can 
> post elements from the Admin menu. Kind of l like the Blog section.
>
> Here's what my custom class looks like in my app's `models.py`.
>
>
> class ProjectLinkPage(Displayable):
>  '''
>  A page representing the format of the page that 
>  has links to standalone, projectlink projectlinks
>  '''
>  categories = models.ManyToManyField("ProjectCategory",
>  verbose_name=_("Categories"),
>  blank=True, related_name="projectlinkposts")
>  # allow_comments = models.BooleanField(verbose_name=_("Allow comments"),
>  # default=False)
>  # comments = CommentsField(verbose_name=_("Comments"))
>  # featured_image = FileField(verbose_name=_("Featured Image"),
>  # upload_to=upload_to("projectlink.ProjectPost.featured_image", 
> "projectlink"),
>  # format="Image", max_length=255, null=True, blank=True)
>  # related_posts = models.ManyToManyField("self",
>  # verbose_name=_("Related posts"), blank=True)
>  # admin_thumb_field = "featured_image"
>
>
>  class Meta:
>  verbose_name=_("Project link")
>  verbose_name_plural=_("Project links")
>  ordering=("-publish_date",)
>
>
>  def get_absolute_url(self):
>  return self.slug
>
> When I open the Admin section, I see "Project links" on the left-side 
> menu. I click it, then click "Add Project Link." I fill out the form and 
> click "save." I then click "View on site." My browser tries to open "
> http://127.0.0.1:8000/admin/r/32/2/,"; but I get a browser error: "This 
> page isn't working. 127.0.0.1 sent an invalid response." My terminal looks 
> like this when I clicked "View on site":
>
> [21/Oct/2017 22:35:39] "GET /admin/r/32/2/ HTTP/1.1" 302 0
> [21/Oct/2017 22:35:39] "GET /admin/r/32/2/ HTTP/1.1" 302 0
>
> I figure I must be defining `get_absolute_url()` wrong. What must I fix so 
> that 'View on site" leads to the right address?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to