Django-cms placeholder in cms template breaks page - python

So I moved my cms templates to a /cms folder in the templates folder:
templates/cms/default_template.html
I edited the settings.py as follows:
CMS_TEMPLATES = (
('cms/default_template.html', 'Default Template'),
)
My template looks like this:
{% extends "base.html" %}
{% load cms_tags %}
{% block base_content %}
{% placeholder template_1_content %}
{% endblock %}
Problem is it throws the following error on page load:
TemplateDoesNotExist at /en/
Error during template rendering
3
4 {% block base_content %}
5 ***{% placeholder template_1_content %}*** -> This line is red indicating the problem is here
6 {% endblock %}
If I remove the {% placeholder template_1_content %} the page loads fine no problems.
It's only after adding that placeholder that it goes bonkers for no reason I can think of.
Has anyone seen something like this before?
BTW the page will load with the placeholder if I don't use the /cms folder.
So what is it about the placeholder and the folder location???
Any help would be appreciated.

I figured it out. Turns out the name of the first template created was stored in the database and that was being referenced. I had to manually edit the database to the new location and name of the template

Related

Django, following a tutorial and there's an issue where my {% block content %} is not being recognized

Here's the dir structure:
directory structure
If the image isn't displaying here's a text version:
templates
myapp
new_search.html
base.html
base.html code:
{% block content %}
{% endblock content %}
new_search.html code:
{% extends "base.html" %}
{% block content %}
<h2>NEW SEARCH</h2>
{% endblock %}
I can display the base.html fine.
The new_search.html however displays like this:
new_search.html
I also want to mention a side question. My Django server isn't running however I can still open the html in my browser. Is that supposed to happen? My URLs when opening both html's are:
base.html:
http://localhost:63342/Full-Stack%20Web%20App/myapp/templates/base.html?_ijt=rtiq5iv3jude6ijjmu0ept2i82
new_search.html:
http://localhost:63342/Full-Stack%20Web%20App/myapp/templates/myapp/new_search.html?_ijt=rtiq5iv3jude6ijjmu0ept2i82
Also, i'm getting this error when trying to visit my home page:
error screenshot
EDIT:
Adding more info. I now get this error. And my new_search template still isn't including the base template. I tried moving the html to different directories etc. In the tutorial it matches too.
template Doesn't Exist
it says the source doesn't exist but I can see it right there. proof
You might want to try copying "base.html" in templates --> myapp folder.
Basically maintain both hmtl files in the same directory.
And also try giving full path after copying them in same dir, like:
{% extends "myapp/base.html" %}
For the home page, you need to have the tag {% load static %} to be able to use the static tag.
Make sure you are using the render() function in your view so it doesn't just return the template as text
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/

Django template tag argument count error

I have a project running on Django 1.6 that is being developed on local machines with a git repo on BitBucket.
We have some test environments running on a server where we then checkout the repo and run the software.
When loading the login page one of the test environments presents the following error:
TemplateSyntaxError at /login/
breadcrumbs requires 0 arguments, 1 provided
In template /.../templates/includes/breadcrumbs.html, error at line 6
The project contains the following code:
admin/login.html
{% extends "workflow/base_site.html" %}
<!-- ... -->
{% block breadcrumbs %}{% endblock %}
workflow/base_site.html
{% extends "workflow/base.html" %}
<!-- ... -->
{% block breadcrumbs %}
{% include "includes/breadcrumbs.html" %}
{% endblock %}
breadcrumbs.html
{% load tags %}
<div class="breadcrumbs">
{% if crumbs %}
{{ crumbs | breadcrumbs:current_object_id | safe }}
{% endif %}
</div>
tags.py
from django import template
register = template.Library()
#register.filter
def breadcrumbs(obj=None, current_object_id=None):
# ...
pass
The login.html template extends the workflow/base_site.html but overrides the breadcrumbs block, and shouldn't be including breadcrumbs.html.
For the login page the breadcrumbs.html template is not expected to have a context variable named crumbs, and shouldn't be calling the breadcrumbs() tag.
The errors are only occurring in one of out test environments, i.e. the same code runs on the same machine under a different user account.
Is anyone able to identify the cause of this problem?
You might have another filter named breadcrumbs in your project. Try renaming yours to something else to check.
It can also be a compiled .pyc file, as you later found out.

Display all model data inside html page

I'm trying to build a website that has products and categories.
When you are on the page of a product, you can click a button to see a list of all the categories it falls under.
You can click another button, that appears on all pages, to see a list of all the categories overall.
In the html page see_all_categories, I wrote a simple block like this:
{% extends 'base.html' %}
{% load staticfiles %}
{% block content%}
{{Category.all}}
{% endblock content %}
I expect to see a messy printout of all the categories but I don't. It doesn't return an error, but it produces nothing, other than the base.html.
What am I doing wrong?
You want to display a list of the categories. I assume your Category model owns an attribute named "title" which is the representation of your Category.
If you're using Django template engine or Jinja2, you can make a for loop inside your template like this :
{% for cat in Category.objects.all %}
{{ cat.title }}
{% endfor %}
As a troubleshooting, I'd suggest you didn't pass your Category model to your template, that is not done automatically. You have to add your model to the context before rendering the template.
As mentionned in the comments, here is doc for template rendering with Django templates.
Django Template Guide
To add your model to the context you can follow this guide.
I don't intend to help you further because I lack of information and that may vary a LOT according to your settings. (Class Based views ? Function based views ? What kind of template are you using... And so on)
I figured out the solution after many long annoying hours of trying everything. I feel dumb but I want to spare the next guy the massive pain in the two-pack.
This is what I did:
In the Views.py, I changed the view function for this page FROM this:
def view_all_categories(request):
context = {'Category' : Category}
return render(request, 'store/see_all_categories.html', context)
TO this
def view_all_categories(request):
all_cats = Category.objects.all().order_by('id')
context = {'all_categories' : all_cats}
return render(request, 'store/see_all_categories.html', context)
and in the page see_all_categories.html itself, I changed it (from the question) TO this:
{% extends 'base.html' %}
{% load staticfiles %}
{% block content%}
{% for cat in all_categories %}
<p>{{ cat.name }}</p>
{% endfor %}
{% endblock content %}
And now it works!!

Is it possible to load a custom template tag in base and use it in extented templates

I loaded a custom template tag note_extras.py in base.html.
base.html
<div id="wrap">
{% load note_extras %}
{% block content %}
{% endblock %}
</div><!--wrap-->
but it is not accessible at templates which is an extend of base.html ie::
home.html
{% extends "base.html" %}
{% block content %}
<div class="container">
{% create_tagmenu request.user.pk %}
</div>
{% endblock %}
it is working fine if i load note_extras in home.html ie:
{% extends "base.html" %}
{% load note_extras %}
....
In Django template language, you must load all needed template libraries in each of the templates.
I personally think it is a good idea because it makes templates more explicit (which is better than implicit). Ill give an example. Prior to Django 1.5, the default behavior for a url tag was to provide the view name in plaintext as well as all the needed parameters:
{% url path.to.view ... %}
There however was no way to provide the path to the view via a context variable:
{% with var='path.to.view' %}
{% url var ... %}
{% endwith %}
To solve that, starting with 1.3, you could import the future version of the url tag (which became the default in 1.5) by doing:
{% load url from future %}
{% url var ... %}
or
{% url 'path.to.view' ... %}
Now imagine that you would need to create a template which would extend from a base template which you did not create (e.g. one of django admin base templates). Then imagine that within the base template it would have {% load url from future %}. As a result, {% url path.to.view ... %} within your template would become invalid without any explicit explanation.
Of course this example does not matter anymore (starting with 1.5) however hopefully it illustrates a point that being explicit in templates is better than implicit which is why the currently implementation is the way it is.
If you want that a template tag is loaded in every template you want to do it in the init file of your app:
from django.template.loader import add_to_builtins
add_to_builtins('my_app.templatetags.note_extras')
In case anyone was wondering, add_to_builtins has been deprecated but one could still load a tag for all of the templates in the project via settings.TEMPLATES - supported for Django 1.9 onwards as described here:
https://stackoverflow.com/a/59719364/2447803

Getting different django administration title

I am trying to get a different header in django administration. I would like to put the company's name there instead. I am trying to do it through the docs. https://docs.djangoproject.com/en/1.5/intro/tutorial02/
Near the bottem, it says to add a TEMPLATE_DIRS setting, which I did.
So, if I have:
'/LPG/firstproject/firstproject/templates',
in my TEMPLATE_DIRS
and this is where the django source file of base_html is at
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/
What exactly does it mean when it says "Now copy the template admin/base_site.html from within the default Django admin template directory in the source code of Django itself"
Is this done with a command or how exactly do I do this?
Try this:
Inside your main template folder, create an admin folder. Inside it, create a file named base_site.html with the following content:
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}
{{ title }} | {% trans 'Your Site Title' %}
{% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'Your Site Title' %}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
Basically, if you want to override a django admin template, you have to match the path for the template and then create your own custom template.

Categories