首页 > 解决方案 > How to make the document files downloadable from wagtail?

问题描述

I have uploaded pdf's to my wagtail page but I cant work out how to get them to actually be downloadable from the web page itself. This is my code for the models.py file.

What html code do I need to make these?

class ArchitectPage(Page):
   pdf_files = models.ForeignKey(
       'wagtaildocs.Document',
       null=True,
       blank=True,
       on_delete=models.SET_NULL,
       related_name='+'
   )

   search_fields = Page.search_fields + [

   ]  # these are if adding a search to the website

   # content tab panels
   content_panels = Page.content_panels + [
       MultiFieldPanel(
           [InlinePanel('architect_pdf', max_num=20, min_num=0, label="architect pdf")],
           heading="architect pdf"
       ),
   ]

   # what to call the panels on wagtail
   edit_handler = TabbedInterface([
       ObjectList(content_panels, heading='Content'),
       ObjectList(Page.promote_panels, heading='SEO'),
       ObjectList(Page.settings_panels, heading='Settings', classname='settings'),
       # classname settings adds the cog
   ])


class ArchitectDownloads(Orderable):
   page = ParentalKey(ArchitectPage, on_delete=models.CASCADE, related_name='architect_pdf')
   architect_pdf = models.ForeignKey(
       'wagtaildocs.Document',
       null=True,
       blank=True,
       on_delete=models.SET_NULL,
       related_name='+'
   )

   panels = [
       DocumentChooserPanel('architect_pdf'),
   ]

标签: pythondjangowagtail

解决方案


<ul>
    {% for download in page.architect_pdf.all %} {# loop over the ArchitectDownload objects #}
        {% with doc=download.architect_pdf %} {# retrieve the Document object for each one #}
            <li><a href="{{ doc.url }}">{{ doc.title }}</a></li>
        {% endwith %}
    {% endfor %}
</ul>

推荐阅读