Adding Wagtail to an existing Django app - django

I'm quite new to Django and Wagtail, and I'm having some difficulty with what I think is a very basic use.
How do I allow Wagtail to edit an existing view's template, while serving that template using Django's serving mechanism?
Assume I have an app (HomePage) created to serve the site's main index (/). I have the HomePage's views set up to render template and certain elements dynamically. Now I want that template to be editable via Wagtail's CMS interface. Something as simple as an image on the frontpage, or a headline.
The closest I've gotten so far has been to follow the Wagtail beginner's tutorial to override the base HomePage class in my app's models.py. That only made my pages available via the /pages/ URL.
Thank you for any help.

Since your site's home page is not a Page object in the Wagtail sense, I'd suggest looking at Wagtail's facilities for managing non-page content - snippets and ModelAdmin would be possible candidates, but I reckon the site settings module would be the best fit.
A Setting model gives you a set of fields which can be configured for display in the Wagtail admin using a 'panels' definition, just like you'd get for a page model - with the important property that only one settings record exists per site. You can retrieve this record within your homepage view or template as shown in the docs, and output it on your template as desired.

One way do that, is to let Wagtail serve your homepage. You will need to change your project's url configuration accordingly, to make wagtail's urls serve the root of your site.
Then, you can pack your dynamic content into a custom template_tag and include in your homepage html template.

Related

Can I access my website from Django admin panel?

I am trying to develop a website where I can see the homepage as it is from Django admin panel. The reason I want that is I want to edit the name of the static content like 'Home' to 'All' along with the change of slide show picture. In summary, I want to change the website layout from Django admin panel that is not accessible to users. Is that possible? I just want an idea of how to implement the process.
Static texts you can change in admin panel with different modules:
for example django-rosetta or my own library Django-tof. https://github.com/wP-soft-GmbH/django-tof
But in your case, i think, you want to made something more.
For this case you can use django-flat-pages, already included in Django, if you have a static web-page.
you can edit every element on the page and after save, you can see it on the front.
if you really want to change the django templates, which you use in your views, you can create a simple template editor in the admin panel based on a widget like django-Ckeditor.

How to import a Wagtail page on all the other wagtail pages

I want to add some content on one of my Wagtail pages and I am trying to import that Wagtail page on all my other wagtail pages. The reason I am trying to do this is that if in the future I make a change on the content it should consistently reflect on all the other Wagtail pages.
Is there a way that I can import a Wagtail page on all my other Wagtail pages, if so please let me know.
Thanks in advance!
I have a website which has the following Configurations:
1) Django-2.0.8
2) Wagtail-2.2.4
A custom template tag is a good way to achieve this, as it provides a place to run custom Python code (for retrieving the necessary data) before outputting the results to the template, either directly as a string or by rendering a template. For example, if you had a footer_text field on a HomePage model, and wanted to display the footer text of the HomePage with slug 'home' on every page, you could define a custom tag as follows:
#register.inclusion_tag('myapp/includes/footer.html')
def footer():
homepage = HomePage.objects.get(slug='home')
return {'footer_text': homepage.footer_text}
You could also look at Wagtail's site settings module as a way to define global content to be re-used across a site (although it's missing a few features that you'd get from defining it on a page model, such as moderation workflow and revision history).

Wagtail add page between two existing pages

I'm using wagtail in a django application with a graphql interface. Wagtail is only the backend and I'm implementing an editor with vuejs and a graphql interface using graphene.
I'd like to add a new page between two existing pages. The page order depends on the path parameter of the page. Is it possible to achieve this without manipulating the path param?
I can change the path param of the pages in wagtail, but this seems to me to be error prone and not as the creators of wagtail intended to be done.
Wagtail uses Django treebeard to enforce the tree structure of pages, and the Wagtail Page model subclasses the Treebeard Node model. I would strongly discourage manipulating the path parameters manually, that could completely mess up your structure. Instead use the Treebeard API to add/remove/move pages/nodes around.
The add_sibling method seems like it'd be the most appropriate here, used like this:
new_page = MyPageModel(
title='My Title'
slug='foo'
...
)
page = Page.objects.get(path='0001...')
page.add_sibling('right', instance=new_page)
Your new_page will be inserted to the right of your chosen page, and all other pages after it will be shifted to the right.

wagtail pages vs using django views and urls

When and where should I use wagtail pages and when should I write my custom urls and views using django? for example should I create pages for user profiles or should I add a urlpattern for profile and write profile logic in a django view?
This question is pretty broad and it depends a lot on what you are trying to achieve, but here are some thoughts.
General Approach
Use Wagtail pages if your pages have CMS (user) editable content and need to be placed (and easily moved) within the main page tree along with other editable pages.
If a page is not editable and URL must be fixed, it might be best to approach this either with Django URLs and views. Eg. Sitemap or Search page.
The docs cover how to set up mixed URLs in Integrating into Django.
Specific Use Case - User Profiles
You could create a Wagtail page model like UserProfileIndex which could contain some CMS editable content about that page such as body text and images.
This assumes you are wanting to make a social media type index and page for each user.
Then use RoutablePageMixin to have the URLs for each individual profile page.
Sometimes this index page thinking can come in handy, it let's you change the root URL but puts the logic for sub-urls within the model.
Remember that this means CMS users could technically change the slug of this page, unless it is locked down.
If you are trying to create a user profile page that is more suited for a user to manage their profile then it would probably best to use Django view.

Django model layout with multiple columns

My admin page for a specific model has two stackedInlines. Currently they display one under the other. I would like them to display side by side so The page would look like this (don't have enough reputation to embed the image :[ )
Any easy way to go about this without having to write my own admin page?
If that's the only solution, how exactly would I go about that?
you can override Django admin template for each app in your project.
you just have too create same folder as your app in template directory and overriding html.
How to override and extend basic Django admin templates?
You can add CSS/Javascript to admin page by defining class Media.
You'll have to override the admin template to accomplish this. See roshan's answer for more info.