How to avoid that Django adds url link to every html element that follows the link - django

i would like to achieve something very basic in Django but can't find out what I am doing wrong. On my apps "index.html", I would like to add a button which redirects to another html template ("site.html") with other content. I added the following to "index.html" which is working:
<body>
<h2>foo</h2>
{% block content %}
<button><a href="{% url 'site' %}"/>Click</button>
{% endblock %}
<p>bar</p>
</body>
Clicking on the button gets me to "site.html", however all html items which I add on "index.html", for example the paragraph "bar" would also get rendered as hyperlink.
I tried creating different Django blocks or making different html sections but that doesn't fix it.
Thank you for your help.

You're missing a closing anchor </a> tag after your element.
<body>
<h2>foo</h2>
{% block content %}
<button>Click</button>
{% endblock %}
<p>bar</p>
</body>
I will note that I'm not sure it's "correct" to have an anchor tag within a button. I think you're better off styling your anchor tag to appear as a button.

Related

Do I have t paste google analytics on evey page of my dgango app or just the base template?

Do I have to paste my ananlytics code into every page of my django app or can I just do it in one spot, the base, similar to disqus?
You can do it in the base page and that will be included in every actually generated page.
You can have, for example, a master_page.html in which you put the main wrapper HTML including your Google Analytics code. The main part of your master page would have:
<html>
<head>
<!-- Google Analytics code -->
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Then your content page will have something like:
{% extends "master_page.html" %}
{% block content %}
Your content for the page.
{% endblock %}

Using same angular js controller twice with django template on same page

I tried to find the solution to this problem on stackoverflow and google but couldn't find it. The issue is using same controller twice on the same page and only the first controller mention gets invoked.
So I have a base template and main page. Now mainpage inherits two completely different blocks - sidebar block and main content block. both needs my controller - myController but the moment I use ngController with same controller name on the page twice on these two completely different divs only the first one gets executed.
Gist: https://gist.github.com/keshavagrawal89/356bb68068ac3ed4ae4e#file-samecontroller
<!-- base.html -->
<div>{% block sidebar %}{% endblock %}</div>
<div>{% block content %}{% endblock %}</div>
<!-- MainPage.html -->
{% block sidebar %}
<ul ng-app="myApp" ng-controller="myController">
<li></li>
</ul>
{% endblock %}
{% block content %}
<div ng-app="myApp" ng-controller="myController"> my page content</div>
{% endblock %}
What am I missing?
You cannot use multiple ng-apps in the same application. Ideally you would just put it in the root of your app or create an app including multiple apps and place it in the root, in the example below all the entities registered in both the apps will be loaded into the myApp module.
ex:-
angular.module('myApp', ['App1', 'App2']);
But in your case it seems like your app may or may not be the same, so best way would be to manually bootstrap your app.
But remember when manually bootstrap your app it is generally not to use ng-app
angular.element().ready(function() {
angular.bootstrap(elmRoot, ['myApp']);
});
Plnkr
Note: You should not use the ng-app directive when manually bootstrapping your app.
the only problem with your code is multiple ng-app's as PSL says on the comment.
ng-app declares the scope for DOM objects for angularjs to parse and it should be use once
typically in the html tag like
<html ng-app="app"> or <html ng-app>
i recommed using a name for the app module
here is a working example

include static files from django template

I'm trying to include a static html page from a django template.
I tried using {% include the_static.html %} but this doesn't work for some unknown reason.
the_static.html page is a data page that will be modified often with an html editor.
and my_model has a url to this html and include it. But django refuses to find it although I'm sure I've setup the path correctly.
You can write your custom template tag to do this.
Create a file named includestatic.py under appname/templatetags/. Also, remember to create appname/templatetags/__init__.py, to include the app in settings and to restart the server.
includestatic.py should have this code:
from django import template
from django.contrib.staticfiles import finders
from django.utils.html import escape
register = template.Library()
#register.simple_tag
def includestatic(path, encoding='UTF-8'):
file_path = finders.find(path)
with open(file_path, "r", encoding=encoding) as f:
string = f.read()
return escape(string)
To use it in your template, put {% load includestatic %} at the top of your template, and then use the tag like {% includestatic "app/file.txt" %}.
I am not sure I understand everything yet...
You've an HTML page served by Django on a given url, let's suppose it to be http://mydjangodomain/get_the_static/. This URL is set in the urls.py of your model. Ok, that's normal.
You have a django template for this model. Let's suppose it's defined in a template directory mytemplates/mymodeltemplates/ and it's called myfrontpage.html (since in Django templates are html files).
I guess you've an URL defined in your urls.py to server that front page ? Let's suppose it's http://mydjangodomain/get_the_front_page/
Now I don't understand how your front page use your static html. Do your final front page html need the static's URL for a "src" attribute or something like it, or do you need to include the static's html into the front page's html ?
In the 1st case, you already have the URL, it's http://mydjangodomain/get_the_static/ so just use it as if.
In the 2nd case, you don't need the previous URL, get ride of it. Furthermore, put the_static.html in mytemplates/mymodeltemplates/. Then you need the {% include "/mymodeltemplates/the_static.html" %} tag. If this doesn't work, make sure you've the following in your settings:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
APPLI_ROOT_PATH = "<absolute_path_to_the_application_root_on_your_server>"
TEMPLATE_DIRS = (
'%s/mytemplates' % APPLI_ROOT_PATH,
)
Sort of resurrecting the dead, but at least with django 1.10, there's a very clean answer here:
http://www.effectivedjango.com/tutorial/static.html
an excerpt from that page:
Simple Template Inclusion We want to add the Boostrap CSS to all of
our templates, but we’d like to avoid repeating ourself: if we add it
to each template individually, when we want to make changes (for
example, to add another stylesheet) we have to make them to all the
files. To solve this, we’ll create a base template that the others
will inherit from.
Let’s create base.html in the templates directory of our contacts app.
{% load staticfiles %}
<html>
<head>
<link href="{% static 'bootstrap/css/bootstrap.min.css' %}"
rel="stylesheet" media="screen">
</head>
<body>
{% block content %}
{% endblock %}
<script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>
</body>
</html>
base.html defines the common structure for our pages, and includes a
block tag, which other templates can fill in.
We’ll update contact_list.html to extend from base.html and fill in
the content block.
{% extends "base.html" %}
{% block content %}
<h1>Contacts</h1>
<ul>
{% for contact in object_list %}
<li class="contact">{{ contact }}</li>
{% endfor %}
</ul>
add contact
{% endblock %}
Having followed this exactly, I now have a base.html that includes all my style references and the navigation bars/etc, so the html in the block content is merely the central contents of each (varying) page.
Do you mean that you want to EXTENDS the parent template the_static.html?
If yes, you should add below code at the first line of your children template:
{% extends "the_static.html" %}
Details documentation can be found here

Django like button not showing

after following the instructions here I have set up django-facebook-like app but the problem is that I cannot see any like button on my page. is there a particular tag I must use in order to display the button or does the button automatically display at the bottom of every page after setting up. I am confused about the last part though. I dont really understand this addition. Can anyone help me. What am I missing
base.html
</head>
{% load like_button %}
<body>
{% like_button_js_tag %}
</body>
</html>

Django template inclusion

The template inheritance page on the django site doesn't really solve my problem (Django 1.2).
My base page looks like:
...
<div class="grid_12" id="content">
{% block content %}{% endblock %}
</div>
...
{% block javascript %}{% endblock %}
I have another template that defines content for these:
{% block content %}
animated sidebar
{% endblock %}
...
{% block javascript %}
alert('hello');
{% endblock %}
This is something like an animated sidebar, so I don't want to extend the base template since it's auxiliary to the main content of the page. If I just use "include", the entire thing is put where the "include" tag is placed - as a result the javascript doesn't run because it's included before one of its dependencies.
What's the best way to solve this?
EDIT
Sorry, I didn't make myself clear.
I have my content pages which render a template that extends "base.html". In "base.html" I want to include a sidebar template that needs to append blocks in "base.html". So I've tried just putting include "sidebar.html" into "base.html", but it just inserts the whole thing where the "include" tag is. What I want it to do is append the blocks in "base.html", which may themselves have been populated by "page.html".
Maybe it's important to say that "sidebar.html" is entirely static - i.e. there's no callable associated with it. So perhaps this question should really be "How can I include a static template into base.html so it will append to blocks in base.html regardless of the output of the actual view that processes the request?"
I think you mean you want to append to a block? You can put {{ block.super }} where you want the inherited content to go. e.g.:
{% block javascript %}
{{ block.super }}
alert('hello');
{% endblock %}
You should only use {% block foo %} tags to extend blocks in a base template, so I'm not clear what you mean when you say you don't want to extend it.
The code, as you've entered it, should render to
...
<div class="grid_12" id="content">
animated sidebar
</div>
...
alert(hello)
Unless you want to append the content (as in Matt's answer) it's not clear what you want to happen.
You shoud be using something like jQuery to trigger execution only after the page is fully loaded. Include jQuery library in the document header and then somewhere:
$(document).ready(function() {
//your code goes here
});