Wagtail (Django) page not displaying due to TemplateDoesNotExist Error - python

I have recently installed Wagtail CMS, into an existing Django Project.
I have created a new App in my existing Django project, and placed the following into the models.py.
from __future__ import unicode_literals
from django.db import models
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
class HomePage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
]
If I visit the Wagtail CMS Admin, and then go to Explorer, and Add Child Page, I now see 'Home Page' as a new Page option. As desired.
However I have two problems: -
Firstly, only the title field is editable. The body field is not clickable.
Secondly when I publish the new page I get a template error: -
TemplateDoesNotExist at /pages/test/
blog/home_page.html
I have checked the documentation here:
http://docs.wagtail.io/en/v1.9/getting_started/tutorial.html
and it states the following:
The page template now needs to be updated to reflect the changes made
to the model. Wagtail uses normal Django templates to render each page
type. By default, it will look for a template filename formed from the
app and model name, separating capital letters with underscores (e.g.
HomePage within the ‘home’ app becomes home/home_page.html). This
template file can exist in any location recognised by Django’s
template rules; conventionally it is placed under a templates folder
within the app.
I created a home_page.html file in the blog app I created, and placed the following code into it:
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block body_class %}template-homepage{% endblock %}
{% block content %}
{{ page.body|richtext }}
{% endblock %}
But I am still getting the same error.
Can you advise how I can resolve template error, and (if connected) why the body field is not editable.
Thanks.

Related

html forms are not showing in the template browsing

[solved]
i am trying to make a blog in django and on the github here is the code
i am trying set the html crispy form template in change password option and made some file names password_change_form.html, password_change_done.html etc. but when try to browse http://127.0.0.1:8000/accounts/password_change/done/ or any kind of pages related to password change section it is not showing the crispy form. but login or signup links are showing in crispy form. password change forms are showing in basic django form.
i want to change it into the desired ones.
i made two apps : blogapp and accounts. i am copying the urls below:
blogapp/urls.py:
from django.urls import path
from .views import (BlogappListView,
BlogappPostView,
BlogappCreateview,
BlogappUpdateView,
BlogappDeleteView,
)
urlpatterns = [
path('post/<int:pk>/delete/',BlogappDeleteView.as_view(),name='post_delete'),
path('post/<int:pk>/edit/',BlogappUpdateView.as_view(),name='post_edit'),
path('post/new/', BlogappCreateview.as_view(),name='post_new'),
path('post/<int:pk>/',BlogappPostView.as_view(),name='post_detail'),
path('',BlogappListView.as_view(),name='home'),
]
accounts/urls.py:
from django.urls import path
from .views import SignUpView
urlpatterns = [
path('signup/',SignUpView.as_view(),name='signup')
]
blog_project/urls.py:
from django.contrib import admin
from django.urls import path,include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/',include('accounts.urls')),
path('accounts/',include('django.contrib.auth.urls')),
path ('',include('blogapp.urls')),
path('',TemplateView.as_view(template_name='home.html'),name='home')
]
i pur password change forms htmls files under templates/registration folder. here is onehtml file for example:
{% extends 'base.html' %}
{% block title %}Forgot Your Password?{% endblock title %}
{% load crispy_forms_tags %}
{% block content %}
<h1>Forgot your password?</h1>
<p>Enter your email address below, and we'll email instructions for setting
a new one.</p>
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-success" type="submit" value="Send me instructions!">
</form>
{% endblock content %}
this file is named as password_reset_form.html. there are are couple of othe files named password_change_done.html, password_reset_complete.html etc none of the password related files html are not showing ....all the urls are showing the basic django template.
i just can't figure out what am i missing and or what did i wrong? password change suppose to show the html form....not in django basic form.
please let me know where is my mistake.thanx in advance
I solved it. I changed templates directory from blogapp to project folder directory where manage.py exists. And everything else I did right. And it took a lot of time to figure out which is silly. Thanx any way... kept the answer if someone need this

Trouble using a custom tag correctly with Django

Summary
I've been trying to get a custom tag working in Django, but seemingly it wont register correctly.
The index file looks to load correctly, it just complains about the tag not being registered correctly.
What I've done right now is just to put the .py file where I register the tag inside an app that is in the django installed apps part.
Should I be doing something else to ensure the tag registers properly?
Further info
The error I get:
Invalid block tag on line 1: 'show_loans'. Did you forget to register
or load this tag?
The view where I call the tag
index.html
{% show_loans %}
The python file where i try to register the tag
loansTable.py
from .models import Loan
from datetime import datetime
from django import template
register = template.Library()
#register.simple_tag('loansTable.html')
def show_loans():
l1 = Loan()
l2 = Loan()
l1.loanedDate_date = datetime.now
l2.handinDate_date = datetime.now
l2.loanedDate_date = datetime.now
l2.handinDate_date = datetime.now
loans2 = { l1, l2 }
return {'loans': loans2}
loansTable.html
<ul>
{% for loan in loans %}
<li> {{ loan }} </li>
{% endfor %}
</ul>
Folder structure:
-app
--templates
---customTemplates
----index.html
----loansTable.html
--loansTable.py
Thanks for your help.
You don't need to register your tag to a template. You just need to load it from there. Which you are already doing.
Therefore just replace:
#register.simple_tag('loansTable.html')
With this:
#register.simple_tag
You also need to put your custom tags in a templatetags directory. From the docs:
In your index.html you must load template tags by file name where you have registered your template tags.
i.e. if you registered tag inside custom_tags.py file
{% load custom_tags %}
The app should contain a templatetags directory, at the same level as models.py, views.py, etc. If this doesn’t already exist, create it - don’t forget the init.py file to ensure the directory is treated as a Python package.
The error tells you exactly what is wrong: you did not load the tag in the template where you are using it, ie index.html.
{% load loansTable %}
{% show_loans %}
Also, you are confusing your tag types. The tag that renders another template is called an inclusion tag, so you should use that when you register it:
#register.inclusion_tag('loansTable.html')
def show_loans():
...

Customize template tags in Django to filter featured_posts in a blog

I have taken a hint from this post
Customising tags in Django to filter posts in Post model
I have created the template tag but I am not sure how to use it in my html. I have a home.html where I want to show three featured post. I am looking for something like {% for post in featured_post %} and then show the post detail.
Also, do I necessarily need to create a featured_posts.html as in the above post because I don't want any extra page for the featured post. I just want them to add on my home page in addition to other stuff.
What I am trying to do is I have created a template tag as under
from django import template
register = template.Library()
#register.inclusion_tag('featured_posts.html')
def featured_posts(count=3):
if Post.is_featured:
featured_posts = Post.published.order_by('-publish')[:count]
return {'featured_posts': featured_posts}
The problem I am facing here is I can't import the Post model from model. My directory structure is somewhat like this:-
I have an app named posts.
Inside that I have models.py and templatetags module and inside the template tag I have blog_tags.py
I couldn't do the relative import.
And then created a new page featured_posts.html as under:-
<ul>
{% for post in featured_posts %}
<li>{{ post.title }} </li>
{% endfor %}
</ul>
Now, I want to use it in my home.html. How can I use it?
Edit:- As mentioned above I could load the models as under:-
from posts.models import Post
home.html
{% load blog_tags %}
{% featured_posts %}
Call your tag. That's it.
or
{% featured_posts count=15 %}
Note, featured_posts here is not the post list (which is iterated in for loop) from context but function name: def featured_posts(count=3). They have the same name in your code and probably this has confused you a little.

Override django change_form.html template when a third app is overriding it

I am trying to override change_form.html only for one model, so I did as explained in the django documentation and created /templates/my_app_name/my_model_name/change_form.html.
The problem is, it is not working and I am not seeing the extra features that I have added to the change_form template. I am using django_guardian, which also overrides the same template, so my assumption is that this is causing the issue. It is worth mentioning, that before placing the template in the my_app_name/my_model_name/ folder, the features of both templates were visible in the admin interface.
Is there a way to apply this only for 1 model?
You should check this answer.
You can specify which template to extend in your template.
So you're extending third app's template which extends djando admin's template.
Example:
admin.py
class MyModel(ThirdAppModelAdmin):
change_form_template = 'my_app_name/templates/change_form.html'
my_app_name/templates/change_form.html
{% extends "thirdapp/templates/change_form.html" %}
{% block thatYouNeed %}
{{ block.super }}
Your content here
{% endblock %}
Well, at least this is how I managed to do that.
The GuardedModelAdmin changes change_form_template to use the template from django-guardian. Try changing it back in your model admin class:
class MyModelAdmin(GuardedModelAdmin):
...
change_form_template = 'my_app_name/my_model_name/change_form.html'

Getting Site_ID in template Django

I have build a web site for a client which has a number of applications. Now he has a new URL registered which he wants to point to the same site, but he wants the look and feel changed. That's basically he wants a new home.html and base.html for the new web site. I can easily add the new site to settings and then change the view for the home page, to display a new home2.html.
However how do I do something like this as expressed in psuedo code in base.html
{% if site_id equals 1 %}
{% include "base1.html" %}
{% endif %}
{% if site_id equals 2 %}
{% include "base2.html" %}
{% endif %}
Any ideas. There are 100s of views on the site and nearly 50 models. I cannot recreate models, and mess around. This needs to be a quick fix.
Thanks in advance
You can create a context processor to automatically add site_id to the context: http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors
But I would opt for a different solution. You can simply add an extra template directory per site so Django will try the templates specifically for that site first and fall back to the normal templates if they're not available.
To extend the idea of WoLph with the context processor, I would maybe even add the switching of the template to the context processor which would clean up your templates, as otherwise you may have to repeat the if clause quite often:
from django.contrib.sites.models import Site
def base_template(request):
site = Site.objects.get_current()
template = "base%s.html" % str(site.pk)
return {'BASE_TEMPLATE': template}
And in your template: {% include BASE_TEMPLATE %}
Looks nicer to me than the switching in the templates!
Another solution would be writing a Middleware to set ´request.site´ the current site id.

Categories

Resources