Python - Django A Simple jQuery Animation

In this tutorial we will create a Simple jQuery Animation the purpose of this tutorial is to show you on how to implement jQuery function. Django is a free and open source web application framework, written in Python. A web framework is a set of components that helps you to develop websites faster and easier. Django makes developers life convenient and productive framework to all. So let's now do the coding.

Getting Started

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. After Python IDLE's is installed, open the command prompt then type "pip install Django", and hit enter. tut1 Wait for the django to be downloaded and installed at the same time. Then After that type "python -m django version" and hit enter to check if django is installed and what version of django is. tut2

Creating the App

After django is set we will now create the web app. While your in the command prompt cd to the directory you want to save the app, then type "django-admin startproject server" and hit enter. A new folder will be created on the directory named 'server'. tut3

Running The Server

After creating the project, cd to the newly created directory, then type "manage.py runserver" and hit enter to start the server running. The "manage.py" is a command of django-admin that utilize the administrative tasks of python web framework. Here is the image of python web server: tut4 Note: Type '127.0.0.1:8000' in the url browser to view the server. When there is code changes in the server just (ctrl + C) to command prompt to stop the server from running, then start again to avoid errors.

Creating The Website

This time will now create the web app to display the web models. First locate the directory of the app via command prompt cd, then type "manage.py startapp web" and hit enter. A new directory will be created named "web". tut5

Setting up The URL

This time will now create a url address to connect the app from the server. First Go to website directory, then open urls via Python IDLE's or any text editor. Then import "include" module beside the url module and import additional module to make a redirect url to your site "from . import views". After that copy/paste the code below inside the urlpatterns.
  1. url(r'^$', views.index_redirect, name='index_redirect'),
  2. url(r'^web/', include('web.urls')),
It will be look like this:
  1. from django.conf.urls import include, url
  2. from django.contrib import admin
  3. from . import views
  4.  
  5. urlpatterns = [
  6.     url(r'^$', views.index_redirect, name='index_redirect'),
  7.     url(r'^web/', include('web.urls')),
  8.     url(r'^admin/', admin.site.urls),
  9. ]
Then after that create a view that will catch the redirect url. To do that create a file "views.py" then copy/paste the code below and save it as "views.py".
  1. from django.shortcuts import redirect
  2.  
  3. def index_redirect(request):
  4.     return redirect('/web/')

Creating The Path For The Pages

Now that we set the connect we will now create a path for the web pages. All you have to do first is to go to web directory, then copy/paste the code below and save it inside "web" directory named 'urls.py' The file name must be urls.py or else there will be an error in the code.
  1. from django.conf.urls import url
  2. from . import views
  3.  
  4. urlpatterns =[
  5.     url(r'^$', views.index, name='index'),
  6. ]

Creating A Static Folder

This time we will create a directory that store an external file. First go to the web directory then create a directory called "static", after that create a sub directory called "web". You'll notice that it is the same as your main app directory name, to assure the absolute link. This is where you import the css, js, etc directory. To get the jQuery framework download it here https://jquery.com/ For the bootstrap framework you get it from here http://getbootstrap.com/

Creating The Views

The views contains the interface of the website. This is where you assign the html code for rendering it to django framework and contains a methods that call a specific functions. To do that first open the views.py, the copy/paste the code below.
  1. from django.shortcuts import render
  2.  
  3. # Create your views here.
  4.  
  5. def index(request):
  6.     return render(request, 'web/index.html')

Registering The App To The Server

Now that we created the interface we will now then register the app to the server. To do that go to the web directory, then open "settings.py" via Python IDLE's or any text editor. Then copy/paste this script inside the INSTALLED_APP variables 'web'. It will be like this:
  1. INSTALLED_APPS = [
  2.     'web',
  3.     'django.contrib.admin',
  4.     'django.contrib.auth',
  5.     'django.contrib.contenttypes',
  6.     'django.contrib.sessions',
  7.     'django.contrib.messages',
  8.     'django.contrib.staticfiles',
  9. ]

Creating The Mark up Language

Now we will create the html interface for the django framework. First go to web directory, then create a directory called "templates" and create a sub directory on it called web. base.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4.     {% load static %}
  5.     <link rel="stylesheet" type="text/css" href="{% static 'web/css/bootstrap.css' %}"/>
  6. </head>
  7.     <div class="navbar navbar-default">
  8.         <div class="container-fluid">
  9.             <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.         </div>
  11.     </div>
  12.     <div class="col-md-3"></div>
  13.     <div class="col-md-6 well">
  14.         <h3 class="text-primary">Python - Django A Simple jQuery Animation</h3>
  15.         <hr style="border-top:1px dotted #ccc;"/>
  16.         {% block body %}
  17.  
  18.         {% endblock %}
  19.     </div>
  20. </body>
  21. <script src="{% static 'web/js/jquery-3.2.1.js' %}"></script>
  22. <script type="text/javascript">
  23.     $(document).ready(function(){
  24.  
  25.         $('#left').on('click', function(){
  26.             $("#box").animate({'marginLeft': '0px'});
  27.         });
  28.  
  29.         $('#right').on('click', function(){
  30.             $("#box").animate({'marginLeft': '200px'});
  31.         });
  32.  
  33.         $('#up').on('click', function(){
  34.             $("#box").animate({'marginTop': '0px'});
  35.         });
  36.  
  37.         $('#down').on('click', function(){
  38.             $("#box").animate({'marginTop': '200px'});
  39.         });
  40.  
  41.         $('#expand').on('click', function(){
  42.             $("#box").animate({'height': '400px', 'width': '400px'});
  43.         });
  44.  
  45.         $('#revert').on('click', function(){
  46.             $("#box").animate({'height': '200px', 'width': '200px'});
  47.         });
  48.  
  49.     });
  50. </html>
Save it as "base.html" inside the web directory "sub directory of templates". index.html
  1. {% extends 'web/base.html' %}
  2. {% block body %}
  3. <div id="box" style="width:200px; height:200px; border:1px solid #000;">
  4.     <center><label>Sourcecodester</label></center>
  5. </div>
  6. <br /><br />
  7. <button id="left" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-left"></span> Left</button>
  8. <button id="up" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-up"></span> Up</button>
  9. <button id="down" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-down"></span> Down</button>
  10. <button id="right" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-right"></span> Right</button>
  11. <button id="expand" class="btn btn-primary"><span class="glyphicon glyphicon-fullscreen"></span> Expand</button>
  12. <button id="revert" class="btn btn-primary"><span class="glyphicon glyphicon-refresh"></span> Revert</button>
  13. {% endblock %}
Save it as "index.html" inside the web directory "sub directory of templates". Now try to run the server again, and see if all things are done. There you have it we successfully created a Simple jQuery Animation. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment