I'm in the process of completing the official Django tutorial and I'm stuck on part 3. Since templates are also used in the last part of part2, I will describe what I did:
Part 2 told me to "copy the template admin/base_site.html from within the default Django admin template directory in the source code of Django itself (django/contrib/admin/templates) into an admin subdirectory of whichever directory you're using in TEMPLATE_DIRS."
So I created a new directory "admin" that has the following relative path (note that where Django uses the directory name 'mysite', I use 'django_test' : /django_test/polls/templates/admin. I copied the base_site.html file into this directory.
When I render the file in my local browser, it says: {% extends "admin/base.html" %} {% load i18n %} {% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %} {% block branding %}{% trans 'Django administration' %}{% endblock %} {% block nav-global %}{% endblock %}
Part 3 has me create an index.html file in a new subdirectory polls/index.html. But when I load this file in my web browser (using localhost server), I simply see the html code instead of a bulleted list (see below).
Note that I also edited TEMPLATE_DIRS in my settings.py file to tell Django that it can find index.html under /Users/myname/Sites/django_test/django_test/templates
Below I will paste the code that my local server renders (instead of the bulleted list, which is what I want). Do you know why this code is being rendered, instead of the bulleted list?
<html>
<head><title>Test</title></head>
<body>
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
</body>
</html>
I don't know whether I'm making a mistake in how I'm organizing the files. Might someone have an idea about what I'm doing wrong?
As you say in your comment, you're putting the file path into your browser. Naturally, then, you're going to see the text of the template, because you are bypassing Django completely and getting the browser to load the unrendered template from disk.
As the tutorial describes, you need to ask Django to serve the template and render it, via its normal URL mechanism. In the earlier part of that section, you went to localhost:8000/admin/ to see the admin site - this hasn't changed just because you've replaced a template. Go back to that address and you'll see your updated - and rendered - template.
The django admin site is easy once you get the hang of it.
The steps to take are:
-Uncomment the django admin site in your urls.py
-Make the css available to the admin site by either copying the admin folder (inside django package) into the folder specified in STATIC_ROOT in your settings.py or making the diectory available on your PYTHONPATH
In other words, you dont need to create a template for the admin site. You will, however, need to create templates to access the views that you create in your project
Related
I was writing my first django program, following instructions from a book. The book wrote url tags like {% url 'learning_logs:index' %} (learning_logs is the app name) but when I tried to emulate that, I got an error until I went with just {% url 'index' %} after reading a post here. Later in my program, after I created another app on the same project called 'users', I was getting the error Reverse for '' not found. '' is not a valid view function or pattern name until I reverted to the initial method used in the book, adding "learning_logs:" before the page names {% url 'learning_logs:index' %}. I need some help on how to recognise when to add the 'name_of_the_app:' and when not to add it in a url tag.
Here is some code sample with the 'learning_logs:' included:
<h1>
Learning Log -
Topics -
{% if user.is_authenticated %}
Hello, {{ user.username }}
{% else %}
log in
{% endif %}
</h1>
{% block content %}{% endblock content %}
And here is how I wrote it with just the url name, which also worked sometimes (This was before I created the new app 'users':
<h1>
Learning Log -
Topics -
</h1>
{% block content %}{% endblock content %}
in the urls.py the function include(that you use to include a new urls.py for an app) has the kwarg namespace which define the namespace of the url for the app, that is what you are looking for. The main reason for this is to not collide too commons urls like "list" or "create" between apps. So if you don't define namespace you can just use the name of the url without namespace: before.
docs here: https://docs.djangoproject.com/en/3.0/topics/http/urls/#url-namespaces
hope you understand.
I am a beginner in Django and i have setup a project with a base.html file along with the folder static/images/img.jpg.
base.html is setup correctly. Everything works inside this file but i have another file called index.html in the templates folder.
So, in other words in the base.html i can use the command {% static '/images/img. jpg' %} but in my index.html Django does not recognise the command.
In the index.html I have already provided the required commands {% extends 'base.html' %} and {% load static %}
Does this command work only in the base html? Do i need another command for files outside the base.html
Thank You
This command should work on every template file of Django, take a look if you are putting the code between a block {% block %} {% endblock %} or even if your IDE don't autocorrect it, run it and see if it works correctly
I'm trying to run project using Django-cms 3.6.0. It was created using Django-cms 2.3.8, I'm trying to use the old code. In this new version the menu with subpages links doesn't appear. children variable in template doesn't seem to contain anything.
I expect it to show links to 4 subpages of a page. I've added pages manually in admin UI in new version of django-cms.
subbase.html:
{% extends "base.html" %}
{% load i18n %}
{% load menu_tags cms_tags %}
...
{% block left_menu %}
<nav id="lMenu">
{% show_menu 1 1 0 1 "menu/sub_menu.html" %}
{% block left_content %}{% endblock left_content %}
</nav>
{% endblock left_menu %}
sub_menu.html:
{% load menu_tags %}
<ul class="subMenu">
{% for child in children %}
<li>{{ child.get_menu_title }}</li>
{% endfor %}
</ul>
I've checked in database, using manage.py shell, that those pages has child pages:
from cms.models.pagemodel import Page
pages = Page.object.all()
children = pages[2].get_descendant_pages()
And now, e.g., calling pages[2].get_menu_title(), children[0].get_menu_title() returns expected proper names of the pages, as added through UI.
I haven't found much about this children variable in docs. Should this still work this way in 3.6? What is the proper way to refer to child pages in template?
I've found, that I had something wrong with language settings. Menu with subpages links started to appear after I:
used i18n_patterns in main project urls.py file
added CMS_LANGUAGES in settings.py according to docs http://docs.django-cms.org/en/latest/reference/configuration.html#cms-languages
added pages in both languages through cms admin page. Before I had page in only one language.
Can't remember exactly which one fixed issue with submenu.
Also maybe something were wrong with urls handling, I used django apps urls from urls.py instead of cms.urls.
Is there a way to include the google-analytics javascript snippet easily into the django admin page ? (django 1.10)
I would like to do that without creating a custom template for each of the admin page, just insert the script in the all admin.
Thank you,
You need to override the base admin page and put the tag there. Inside Template Directory, create a folder name admin, inside that, create a base_site.html. That base_site.html might look like this:
{% extends 'admin/base_site.html' %}
{% load static %}
{% block extrahead %}{{ block.super }} google analytics codes {% endblock %}
Does anybody know a way to adjust the included JS/CSS resources in a template based on the apps you've installed?
Let's say we have a basic feature in app x using template.html, and this requires foo.js which is provided in the static files for the app.
What I'd like is a way of saying an additional and optional app y can register bar.js to be included in template.html as well and this provides some advanced functionality.
Ideally, this should be tied in on a feature level - so I register both foo.js and bar.js to provide for feature A and in my template I just indicate I want all the static content for A.
You can follow the django admin framework approach. In your base template have an extra section for style and javascript. Based on some condition you can insert the new files.
For Example:
Define these two blocks in your base template
{% block extracss %}{% endblock %}
{% block extrajs %}{% endblock %}
If you want to add a js or css based on some condition, you can add a check inside
{% block extracss %}
{% if new_app_installed %}
# Insert your CSS
{% else %}
# Default
{% endif %}
{% endblock %}
You can also check if your plugin app is installed and pass this context variable from view to template.
from django.conf import settings
if "new_app" in settings.INSTALLED_APPS:
is_new_app_installed = True