Django template language extends doesn't work - python

Hi guys I am studying django framework using "django by example" book. The problem I am facing is that the template language does not work even I copy and pasted source code from book and/or github.
A blog application that has post model and is inside the mysite project.
You can see the code in picture and the result too:
Inside blog\templates\blog .
blog\templates\blog\base.html
<!-- its base.html -->
{% load staticfiles %}
<!DOCTYPE HTML>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link href="{% static "css/blog.css" %}" rel="stylesheet">
</head>
<body>
<div id="content">
{% block content %}
{% endblock %}
</div>
<div id="sidebar">
<h2>My blog</h2>
<p>This is my blog.</p>
</div>
</body>
</html>
Its the list that extends base.html and located in blog\template\blog\post:
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
Here is views.py
from django.shortcuts import render, get_list_or_404
from .models import Post
def post_list(request):
posts=Post.published.all()
return render(request,'blog/post/list.html',{'posts':posts})
def post_detail(request, year, month, day, post):
post=get_list_or_404(Post, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request,'blog/post/detail.html',
{'post':post})

Related

Django 'Library' object has no attribute 'simple'

I am following the book "Django by Example", and right at the beginning of Chapter 3: Extending your blog application, I am trying to load my blog_tags file, however I get this AttributeError:
In template /home/ahmad/Documents/Coding/Django By Example/Excercises/djangoblogapp/mysite/blog/templates/blog/post/list.html, error at line 1
'Library' object has no attribute 'simple'
Here is my base.html:
{% load blog_tags %}
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %} {% endblock %}</title>
<link href="{% static "css/blog.css" %}" rel="stylesheet">
</head>
<body>
<div id = "content">
{% block content %}
{% endblock %}
</div>
<div id = "sidebar">
<h2> My Blog</h2>
<p> This is my blog. I've written {% total_posts %} posts so far. </p>
</div>
</body>
</html>
and my post/list.html:
{% extends "blog/base.html" %}
{% block title %}My Blog {% endblock %}
{% block content %}
<h1> My Blog </h1>
{% if tag %}
<h2>Posts tagged with "{{ tag.name }}"</h2>
{% endif %}
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="tags">
Tags:
{% for tag in post.tags.all %}
<a href="{% url "blog:post_list_by_tag" tag.slug %}">
{{ tag.name }}
</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% include "pagination.html" with page=posts %}
{% endblock %}
Lastly, here is my templatetags/blog_tags.py:
from django import template
register = template.Library()
from ..models import Post
#register.simple._tag(name='useless')
def total_posts():
return Post.published.count()
I have exhausted Google and I cannot find any solution to my problem. I would greatly appreciate your help!
In case it helps, I am using Python 3.6.2 with virtualenvwrapper on a Linux system. Django version is 1.8.6
EDIT: New error that I am getting:
In template /home/ahmad/Documents/Coding/Django By Example/Excercises/djangoblogapp/mysite/blog/templates/blog/base.html, error at line 19
Invalid block tag: 'total_posts'
Basically, the error points towards this line of code:
<p> This is my blog. I've written {% total_posts %} posts so far. </p>
I don't see anything else that could be wrong.
if you want to use simple-tags, remove dot
#register.simple_tag(name='useless')
# ^^^^^
def total_posts():
and edit in the base.html,
replace
written {% total_posts %} posts so far.
<!-- ^^^^^^^^^ -->
to the name of simple tag
written {% useless %} posts so far.
<!-- ^^^^^^^^^ -->

Why won't the flask app import the flask-bootstrap base.html template when deployed to Heroku?

I have no problem getting it to work on my local machine but when deployed to Heroku the flask-bootstrap asset won't import the base.html file which is located in the virtual environment. And I can't find any errors in the logs or the console.
Do I still need to activate the virtual environment on Heroku or should that have been taken care of already?
Here's the config.py file where I try to import bootsrap:
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
Bootstrap(app)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'URI-is-changed-on-this-question-for-privacy-reasons'
app.config['SECRET_KEY'] = 'secret-key-is-changed-on-this-website-for-privacy reasons'
app.py imports the files and activates the server
from config import app
from flask import Flask
from models import authentication, posts
from views import authentication, posts
from forms import authentication, posts
if __name__ == '__main__':
app.run()
Here's the html file that tries to extend the boostrap base.html
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block jumbotron %}
<div class="jumbotron">
<h2>Hunter Krieger</h2>
<h3>Blogger, Web Developer, Gamer and Bibliophile
</div>
{% endblock jumbotron %}
{% block content %}
{% if post %}
{% for display in post %}
<article class="well">
<h1>{{ display.title }}</h1>
<h4>Written by {{ display.author }}</h4>
<h4>{{ display.created_at.strftime('%b %d, %Y') }}</h4>
<div class="body">
{{ display.body | truncate(1250) | safe }}
</div>
</article>
{% endfor %}
{% else %}
<p>There are no posts</p>
{% endif %}
{% endblock %}
And here's the base.html file which is located in the virtual environment on the path './venv/Lib/site-packages/flask_bootstrap/templates/bootstrap/base.html'
{% block doc -%}
<!DOCTYPE html>
<html{% block html_attribs %}{% endblock html_attribs %}>
{%- block html %}
<head>
{%- block head %}
<title>{% block title %}Welcome to hckrieger.com!{% endblock title %}</title>
{%- block metas %}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{%- endblock metas %}
{% block favicon %}
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
{% endblock %}
{%- block styles %}
<!-- Bootstrap -->
<link href="{{bootstrap_find_resource('css/bootstrap.css', cdn='bootstrap')}}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/stylesheet.css') }}" rel="stylesheet">
{%- endblock styles %}
{%- block fonts %}
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
{%- endblock fonts %}
{% block texteditor %}
<script src="//cdn.ckeditor.com/4.7.1/full/ckeditor.js"></script>
{%- endblock texteditor %}
{%- endblock head %}
</head>
<body{% block body_attribs %}{% endblock body_attribs %}>
{% block body -%}
{% block navbar %}
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<!--<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>-->
<a class="navbar-brand" href="{{ url_for('index') }}">hckrieger.com</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>--><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
{%- endblock navbar %}
{% block jumbotron %}
{% endblock jumbotron %}
{% if admin %}
<div class="container">
<section class="content col-md-12">
{% block admin_content -%}
{%- endblock admin_content %}
</section>
</div>
{% else %}
<div class="container">
<section class="content col-md-9">
{% block content -%}
{%- endblock content %}
</section>
<section class="content col-md-3">
{% include 'sidebar.html' %}
</section>
</div>
{% endif %}
{% block footer %}
<p class="center">hckrieger.com © 2017</p>
{% endblock %}
{% block scripts %}
<script src="{{bootstrap_find_resource('jquery.js', cdn='jquery')}}"></script>
<script src="{{bootstrap_find_resource('js/bootstrap.js', cdn='bootstrap')}}"></script>
{%- endblock scripts %}
{%- endblock body %}
</body>
{%- endblock html %}
</html>
{% endblock doc -%}
From seeing that code does anybody know what I could change to get flask-bootstrap to work on Heroku?
In your first code snippet, Bootstrap(app) should be bootstrap = Bootstrap(app):
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
bootstrap = Bootstrap(app) # this line
db = SQLAlchemy(app)

NoReverseMatch at /forum/

I am trying to implement the django-registration-redux and have used templates written by Andres available at https://github.com/macdhuibh/django-registration-templates . But the problem is whever i run anything i get the NoReverseMatch Error.
I tried to render the base.html template to check the error and i got error on line 12.
and the base.html is as
{% load i18n %}
<html lang="en">
<head>
<link rel="stylesheet" href="{{ STATIC_URL }}style.css" />
<title>{% block title %}User test{% endblock %}</title>
</head>
<body>
<div id="header">
{% block header %}
{% trans "Home" %} |
{% if user.is_authenticated %}
{% trans "Logged in" %}: {{ user.username }}
({% trans "Log out" %} |
{% trans "Change password" %})
{% else %}
{% trans "Log in" %}
{% endif %}
<hr />
{% endblock %}
</div>
<div id="content">
{% block content %}{% endblock %}
</div>
<div id="footer">
{% block footer %}
<hr />
{% endblock %}
</div>
</body>
</html>
index.html is as
{% extends "base.html" %}
{% load i18n %}
{% block content %}
Index page
{% endblock %}
and the urls.py as
from django.conf.urls import url
from . import views
app_name = 'forum'
urlpatterns = [
url(r'^$', views.index, name='index'),
]
and i get the error as in the below image:
Error
You probably need to specify the app name in the tag:
{% trans "Home" %}

Transferring my website from PHP to Django

I'm transferring my website from PHP to Django, and now I have to make a sidebar.
I want the entries to come from the DB, and than I want them to become hyperlinks for other pages..
How can I do that?
nav.html
<nav class="menu" id="theMenu">
<div class="menu-wrap" data-spy="scroll">
<h1 class="logo">MY BOOKS</h1>
<i class="icon-remove menu-close"></i>
{% for question in latest_question_list %}
{{ question.naslov }}
{% endfor %}
</div>
<div id="menuToggle"><i class="icon-reorder"></i></div>
</nav>
master2.html
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link href="/static/font.min.css" rel="stylesheet">
<link href="/static/bootstrap.min.css" rel="stylesheet">
<link href="/static/font-awesome.min.css "rel="stylesheet">
<link href="/static/main.css" rel="stylesheet">
</head>
<body data-spy="scroll" data-offset="0" data-target="#theMenu">
{% include "nav.html" %}
{% include "header2.html" %}
{% block h1 %}{% endblock %}
<script src="/static/jquery.js"></script>
<script src="/static/bootstrap.min.js"></script>
<script src="/static/jquery.isotope.min.js"></script>
<script src="/static/jquery.prettyPhoto.js"></script>
<script src="/static/main2.js"></script>
</body>
</html>
views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.all()
context = {'latest_question_list': latest_question_list}
return render(request, 'papers/index.html', context)
def detail(request, slug):
question = Question.objects.get(slug=slug)
return render(request, 'papers/detail.html', {'question': question})
urls.py
from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
urlpatterns = [
url(r'^$', 'papers.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<slug>[\w_-]+)/$', 'papers.views.detail', name='detail'),
]
urlpatterns += staticfiles_urlpatterns()
detail.html
{% extends "master2.html" %}
{% block h1 %}
<div id="g">
<div class="container">
<div class="row">
<h3>{{ question.naslov }}</h3>
<br>
<br>
<div class="col-lg-6 desc "><p>{{ question.opsirnije_text }}</p></div>
<div class="col-lg-4 desc desc-b">
<p>{{ question.opsirnije_text }}</p>
</div>
</div>
</div>
</div>
{% endblock %}
{% block title %} Detail {% endblock %}
You might be looking for the for template tag.
{% for question in latest_question_list %}
{{ question.naslov }}
{% endfor %}
Now, if you want the list of questions to show up on all (or almost all) pages, you could write a template context processor that adds them to all contexts.

django template inheritance not working everywhere

I have a blog app and something is really bogging me. I have a base.html template that I extend in every template of my views and that works perfectly, just one of the views, which is the one that only shows the blog post and not the rest of the posts, doesn't extend the base.html even though I have the {% extends 'base.html'%} just as in every other template and everything else basically the same. Also static files aren't loading, even though I load them just as in every other template ..
base.html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="{{STATIC_URL}}style.css">
<link rel="shortcut icon" href="static/favicon.ico" />
<meta charset="utf-8">
<title>
{% block title %}{% endblock %}
</title>
</head>
<p class="header">Blog</p>
<body background="static/landscape.jpg">
<div class="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
Other template(works):
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}Blog {% endblock %}
{% block content %}
{% for post in posts %}
<div class="post">
<h1>
<a class ="title" href="{{post.get_absolute_url}}">
{{post.title}}
</a>
</h1>
<p>{{post.content}}</p>
<hr>
</div>
{% endfor %}
{% endblock %}
Specific template(doesn't work):
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}{{post.title}}{% endblock %}
{% block content %}
<article>
<header>
<h1 style="font-size:40px;"> {{post.title}} </h1>
<p>{{post.content|safe}}</p>
<p class="date">
Posted on
<time datetime="{{post.created|date:"c"}}">
{{post.created|date}}
</time>
</p>
</header>
</article>
<hr>
{% endblock %}
I'd be very thankful if you can discover anything I can't ... thanks.
This really sounds like a path issue to me. Try adding a / to your css and background paths, for example: <body background="/static/landscape.jpg"> and see if that makes a difference.

Categories