How do I properly import templates from HTML5 UP into django - javascript

Im trying to upload templates from HTLM5 UP into the Django framework. However I'm not sure where to place some of the supporting CSS, SASS, Webfonts, Javascript files. Link to the template I downloaded below. Files I'm referring to are in the "Assets" folder.
https://html5up.net/strata
I've tried placing them in the path below. I'm using the Pycharm IDE if that changes anything.
ProjectName/mysite/templates/mysite/assets
I've been trying to call them with href="mysite/assets/css/main.css" with no success.

IDE: PyCharm
At first, you need to {% load staticfiles %} top of the .html file
And link your file like :
CSS = <link rel="stylesheet" href="{% static 'assets/css/main.css' %}" />
img= <img src="{% static 'images/rakib.jpg' %}" alt="" />
Js= <script src="{% static 'assets/js/jquery.min.js' %}"></script>
Full code:
Filename: base.html
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title> {% block head_title %} Blog {% endblock head_title %} </title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<link rel="stylesheet" href=href="{% static 'assets/css/main.css' %}" />
<style>
{% block style %} {% endblock style %}
</style>
{% block head_extra %} {% endblock head_extra %}
</head>

Related

Django - Javascript does not load

I have seen so many questions and answers on this but nothing is working.
I have a base.html which looks like so:
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello Django</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="{% static "css/style.css" %}">
<link rel="stylesheet" href="{% static "css/style2.css" %}">
<script src="{% static 'js/jquery.1.12.4.min.js' %}"></script>
<script type="text/javascript" src="{% static "js/main.js" %}"></script>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
{% block js %}{% endblock %}
</head>
<body>
{% block content %}{% endblock content %}
</body>
</html>
I then have a template which looks like so:
{% extends "base.html" %}
{% block javascript %}
<script>
alert("hello");
</script>
{% endblock %}
{% block content %}
In both cases, 1) The JavaScript file main.js is not loaded in. If I look at the DOM, the JavaScript file is not there. If I add in another CSS file, that works fine. Its just JavaScript.
2) The JavaScript in the block in the template doesn't run either.
Structure:
Your main template declares a block called "js" but your child template has a block called "javascript". These need to be the same.

Can not link JS and CSS to HTML

I run flask on my Mac and try to connect the js and css to my html using the following code. When I inspect the element, it seem like the directory is complete correct but the css and js do not have effect on my html Please help !:
<!-- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta -->
<meta charset="utf-8"/>
<meta content="initial-scale=1, width=device-width" name="viewport"/>
<!-- Script for using MathJax -->
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
<!-- Link to own style sheet -->
<link href="{{ url_for('static', filename='styles.css') }}" rel="stylesheet"/>
<!-- app's own JavaScript -->
<script src="{{ url_for('static', filename='scripts.js') }}"></script>
<title>Corporate Finance Assistant: {% block title %}{% endblock %} </title>
</head>

Django Static js files not working

Well my template code.
<!DOCTYPE html>
<html>
<head>
{% load staticfiles %}
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="{% static 'website/staffaddproblem.js' %}"></script>
<title>Dashboard</title>
<link href="{% static 'website/style.css' %}" rel="stylesheet" type="text/css"/>
</head>
<body>
body
</body>
<html>
As you can see it has a css file. which is working properly, but java script file does not work.
when i load the page its source looks like this.
When i click the link it loads the file too its there its linking fine but its not working . there is no problem in code js code working fine when written in same file.
Have you changed the settings.py file to include staticroot?
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Here's a part of my template which works with both js and css files.
{% load static %}
<html>
<head>
<link type="text/css" rel="stylesheet" href="{% static 'css/materialize.min.css' %}">
<link type="text/css" rel="stylesheet" href="{% static 'css/stylesheet.css' %}">
<script type="text/javascript" src="{% static 'js/jquery-1.11.3.min.js' %}"></script>
<title>
The directories are as follows:
- static
-js
- jquery-1.11.3.min.js
-css
- stylesheet.css
- materialize.min.css
- templates
-index.html

Cannot make Lightbox2 work in Django

I cannot make Lightbox2 (http://lokeshdhakar.com/projects/lightbox2/) work with Django. When I click on member's picture I would like to see the lightbox appear. Instead it displays the downloaded image. Also hover does not work on the image. Here is what I've done so far:
I have downloaded Lightbox2 and copied files in js, img and css folders.
base.html
<head>
<link rel="stylesheet" href="{% static 'css/ligthbox.css' %}" >
</head>
<body>
<link rel="stylesheet" href="{% static 'css/ligthbox.css' %}" >
<link rel="stylesheet" href="{% static 'css/ligthbox.css' %}" >
</body>
member_details.html (Django template)
<div class="members">
{% if userprofile.profile_picture %}
<a href="{{ MEDIA_URL }}
{{userprofile.profile_picture.url|default_if_none:'' }}" lightbox="{{userprofile.user.first_name }}" >
<img class="lightbox" src="{{ MEDIA_URL }}{{ userprofile.profile_picture.url|default_if_none:"" }}" alt="{{ userprofile.user.first_name }}" />
</a>
{% endif %}
</div>
Can someone please suggest how to fix this please.
Whilst you have added some example code there is not enough information in your question to give an accurate answer. There are a number of problems with the code blocks you have included in your question also:
Your example code has numerous spelling mistakes which means your CSS will give you 404.
There is no javascript included
The data attributes on your image <img> and link <a> tags are not named correctly.
Look through the how to use guide not using dynamic data (use a static image) get it working then replace the images with server side template generated content.
HOW TO USE
Step 1 - Load the Javascript and CSS
Download and unzip the latest version of Lightbox.
Look inside the js folder to find jquery-1.11.0.min.js and lightbox.min.js and load both of these files. Load jQuery first.
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/lightbox.min.js"></script>
Look inside the css folder to find lightbox.css and load it.
<link href="css/lightbox.css" rel="stylesheet" />
Look inside the img folder to find close.png, loading.gif, prev.png, and next.png. These files are used in lightbox.css. By default, lightbox.css will look for these images in a folder called img.
Step 2 - Turn it on
Add a data-lightbox attribute to any image link to activate Lightbox. For the value of the attribute, use a unique name for each image. For example:
<a href="img/image-1.jpg" data-lightbox="image-1" data-title="My caption">
Image #1
</a>
Optional: Add a data-title attribute if you want to show a caption.
If you have a group of related images that you would like to combine into a set, use the same data-lightbox attribute value for all of the images. For example:
Image #2
Image #3
Image #4
Thanks #MattWritesCode and #cbergmiller.
Following #MattWritesCode advice I now have in my template:
<div class="row">
<div class="col-md-4 col-xs-6">
<a href="{% static 'img/8marc.jpg' %}" data-lightbox="8marc" data-title="My caption">
<img src="{% static 'img/8marc.jpg' %}" /></a>
</a>
</div>
</div>
base.html
<head>
<title>TrackCat - {% block title %}TrackCat{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans:400,600,300">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<link rel="stylesheet" href="{% static 'css/lightbox.css' %}" >
{% block custom_css %}{% endblock %}
</head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="{% static 'js/jquery.datetimepicker.js' %}"></script>
<script type="text/javascript" src="{% static 'js/lightbox.min.js' %}"></script>
I managed to activate lightbox functionality using
<script type="text/javascript" src="{% static 'js/lightbox.js' %}"></script>
instead of lightbox.min.js in my base.html.

import js in html.twig symfony2

I try to import the JS file into my html.twig file. But it doesn't work. Could you give me some suggestions?
this is my layout file
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{%block stylesheets %}
<link rel="stylesheet" href="{{ asset('bundles/ensjobologic/css/bootstrap.css') }}" type="text/css" media="all" />
<link rel="stylesheet" href="{{ asset('bundles/ensjobologic/css/layout.css') }}" type="text/css" media="all" />
<link href="asset('bundles/ensjobologic/css/bootstrap-responsive.css" rel="stylesheet">
<link rel="stylesheet" href="{{ asset('bundles/ensjobologic/css/flat-ui.css') }}" type="text/css" media="all" />
{% endblock %}
{% block javascripts %}
{% endblock %}
</head>
this is the child file:
{% block javascripts %}
{{ parent() }}
{% javascripts 'js/jquery.tagsinput.js' %}
<script src="{{ asset_url }}" type="text/javascript"></script>
{% endjavascripts %}
{% endblock %}
the css work well. But the js file don't have any effect.
asset() is not assetic. You don't have to speficy the asset in the javascripts macro. See how to use assetic and inlcude javascripts
{% block javascripts %}
{{ parent() }}
{% javascripts '#EnsUserBundle/Resources/public/js/jquery.tagsinput.js'%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
Take notice at the asset_url variable.
EDIT: For dev the assets are delivered by a controller (in default settings), so every change is detected. Only after adding new files a cache clear is needed.
for prod you need to dump them to a physically file under the web path with
php app/console assetic:dump --env=prod --no-debug

Categories

Resources