NoReverseMatch at /forum/ - python

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" %}

Related

How can I use template blocks in django?

I'm trying to load a header template into my index with {% block %} but I can't get it to load
index.html
<body>
<header>
{% block header %}{% endblock %}
</header>
<h1>Hello</h1>
</body>
header.html
{% extends "index.html" %}
{% block header %}
<div class="header">
<i class="fas fa-archive"></i>
<i class="fas fa-home"></i>
<h1>hey</h1>
</div>
{% endblock %}
views.py
def index(request):
categories = Category.objects.all()
context = {'categories': categories}
return render(request, 'category/index.html', context)
The app is installed in the settings.
To get things to work the way you wanted you would need to make header.html the base template then extend index.html off of it. This is because header.html is only substituting in {% block header %} when you render header.html. Index.html doesn't see any of these substitutions when rendered. You may want to make the header.html file a static file and load it in that way. Similar to how you would with css.
To achieve what you want to you should render header.html now, check django docs.
It's better to have a layout and then put the header block inside it.
layout.html
...
<body>
<header>
{% block header %}{% endblock %}
</header>
<h1>Hello</h1>
{% block content %}{% endblock %}
<footer>
{% block footer %}{% endblock %}
</footer>
</body>
...
index.html
{% extends "layout.html" %}
{% block header %}
the header
{% endblock %}
{% block content %}
here the content
{% endblock %}
{% block footer %}
here the footer
{% endblock %}
Now you can render the index.html in your view

Django admin reuse header

I would like to reuse the header of the django admin page in my custom view so I can use the "view site, change password and logout" so I won't have to recode that part.
Is it possible to extend only the header part?
Probably you can't. Because these are integral part of admin/base.html template. So instead, you can create a new base template and copy the header section from this admin/base.html. Like this:
{% load i18n static %}<!DOCTYPE html>
{% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">
{% block extrastyle %}{% endblock %}
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% static "admin/css/rtl.css" %}{% endblock %}">{% endif %}
{% block extrahead %}{% endblock %}
{% block responsive %}
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static "admin/css/responsive.css" %}">
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% static "admin/css/responsive_rtl.css" %}">{% endif %}
{% endblock %}
{% block blockbots %}<meta name="robots" content="NONE,NOARCHIVE">{% endblock %}
</head>
{% load i18n %}
<body class="{% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %}"
data-admin-utc-offset="{% now "Z" %}">
<!-- Container -->
<div id="container">
{% if not is_popup %}
<!-- Header -->
<div id="header">
<div id="branding">
{% block branding %}{% endblock %}
</div>
{% block usertools %}
{% if has_permission %}
<div id="user-tools">
{% block welcome-msg %}
{% trans 'Welcome,' %}
<strong>{% firstof user.get_short_name user.get_username %}</strong>.
{% endblock %}
{% block userlinks %}
{% if site_url %}
{% trans 'View site' %} /
{% endif %}
{% if user.is_active and user.is_staff %}
{% url 'django-admindocs-docroot' as docsroot %}
{% if docsroot %}
{% trans 'Documentation' %} /
{% endif %}
{% endif %}
{% if user.has_usable_password %}
{% trans 'Change password' %} /
{% endif %}
{% trans 'Log out' %}
{% endblock %}
</div>
{% endif %}
{% endblock %}
{% block nav-global %}{% endblock %}
</div>
<!-- END Header -->
</div>
<!-- END Container -->
</body>
</html>

Django template language extends doesn't work

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})

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)

Categories