search results:

    • Standard
    • React
    Pricing Learn Community
    • + D
    • Light
    • Dark
    • System
    logo TW Elements
    • Getting started
      • Quick start
      • Tutorials
      • Design system
      • Local installation
      • Optimization
      • Dark mode
      • Theming
      • Changelog
      • Migrating to v2
      • Internationalization guide
      • Class customization
      • Icons Integration
    • CommercialNew
      • Pricing
      • License
      • Installation
      • Git & repository
      • Premium Support
    • Integrations
      • Angular
      • ASP.NET
      • Django
      • Express
      • Laravel
      • Next
      • Nuxt
      • Qwik
      • React
      • Remix
      • Solid
      • Svelte
      • SvelteKit
      • Vue
    • Content & styles
      • Animations
      • Animations Extended
      • Colors
      • Dividers
      • Figures
      • Headings
      • Hover effects
      • Icons
      • Images
      • Mask
      • Shadows
      • Typography
    • Navigation
      • Breadcrumbs
      • Footer
      • Headers
      • Mega menu
      • Navbar
      • Offcanvas
      • Pagination
      • Pills
      • Scrollspy
      • Sidenav
      • Tabs
    • Components
      • Accordion
      • Alerts
      • Avatar
      • Badges
      • Button group
      • Buttons
      • Cards
      • Carousel
      • Chips
      • Collapse
      • Dropdown
      • Gallery
      • Jumbotron
      • Lightbox
      • Link
      • List group
      • Modal
      • Notifications
      • Paragraphs
      • Placeholders
      • Popconfirm
      • Popover
      • Progress
      • Rating
      • Scroll back to top button
      • Social buttons
      • Spinners
      • Stepper
      • Testimonials
      • Timeline
      • Toast
      • Tooltip
      • Video
      • Video carousel
    • Forms
      • Autocomplete
      • Checkbox
      • Datepicker
      • Datetimepicker
      • File input
      • Form templates
      • Input Group
      • Inputs
      • Login form
      • Multi range slider
      • Radio
      • Range
      • Registration form
      • Search
      • Select
      • Switch
      • Textarea
      • Timepicker
      • Validation
    • Data
      • Charts
      • Charts advanced
      • Datatables
      • Tables
    • Methods
      • Clipboard
      • Infinite scroll
      • Lazy loading
      • Loading management
      • Ripple
      • Scrollbar
      • Smooth scroll
      • Sticky
      • Touch
    • Design Blocks
      • Admin Charts
      • Admin Complex
      • Admin Forms
      • Admin Maps
      • Admin Navigation
      • Admin tables
      • Banners
      • Contact
      • Content
      • CTA
      • FAQ
      • Features
      • Headers
      • Hero / Intro sections
      • Logo clouds
      • Mega menu
      • News
      • Newsletter
      • Pricing
      • Projects
      • Stats
      • Stats admin
      • Team
      • Testimonials
    • Tools
      • Button generator
      • Card generator
      • Flexbox generator
      • Footer generator
      • Form builder
      • Grid generator
      • Icon generator
      • Instagram Filters generator
      • Logo generator
      • Table generator
      • Typography generator
    • Coming Soon
      • Angular
      • Builder
      • Templates
      • Vue
    • Resources
      • Playground
      • YouTube Channel
      • Private FB Group
      • Newsletter
      • UI Design course New
      • UI / UX tips

    TW Elements Django integration

    This article shows you how to integrate Django application with TW Elements. Free download, open source license.


    Prerequisites

    Before starting the project make sure to install the following utilities:

    • Node LTS (18.x.x or higher recommended)
    • Python
    • pip package manager
    • Code editor. We recommend VSCode

    Creating a new Django application

    Let's create a fresh Django application so that we can go through all the steps together.

    Step 1

    Add Python virtual environment.

    • Terminal - Windows
    • Terminal - MacOS/Linux
            
                
        mkdir my-project
        cd my-project
        py -3 -m venv venv
        
            
        
            
                
        mkdir my-project
        cd my-project
        python -3 -m venv venv
        
            
        

    Step 2

    Now activate created environment. After this you should see venv catalogue and in your terminal will be visible (venv) badge.

    • Terminal - Windows
    • Terminal - MacOS/Linux
            
                
        venv/Scripts/activate
        
            
        
            
                
        source venv/bin/activate
        
            
        

    Step 3

    Install Django.

    • Terminal
            
                
        pip install django
        
            
        

    Step 4

    Create src folder in the root directory and there initialize Django project.

    • Terminal
            
                
        mkdir src
        cd src
        django-admin startproject mysite .
        
            
        

    Step 5

    Create a folder called static in the src directory. You can place your own CSS files there. Let's create there a file named input.css. There you can put your own CSS styles.

    Step 6

    Navigate to the settings.py in the mysite folder and update the section with paths for static files.

    • ./src/mysite/settings.py
            
                    
        STATIC_URL = 'static/'
        STATICFILES_DIRS = [
          BASE_DIR / "static"
        ]
        STATIC_ROOT = BASE_DIR.parent / "static"    
        
            
        

    Step 7

    Create a folder named templates in the src directory. Then, open the settings.py file in the mysite folder and update the section with the template paths.

    • ./src/mysite/settings.py
            
                    
        TEMPLATES = [
            {
                ...
                'DIRS': [BASE_DIR / 'templates'],
                ...
            },
        ]  
        
            
        

    Step 8

    Create a file named base.html in the templates directory. This file will be a base for all other pages. Additionally, within this folder, create new named pages, and inside it, create a file named home.html.

    • ./src/templates/base.html
    • ./src/templates/pages/home.html
            
                    
        {% load static %}
        <!DOCTYPE html>
        <html lang="en">
          <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <link href="{% static 'input.css' %}" rel="stylesheet" />
            <title>
              {% block head_title %}TWE Django Integration Tutorial{% endblock %}
            </title>
          </head>
          <body>
            <div>{% block content %} {% endblock %}</div>
          </body>
        </html>
        
            
        
            
                
        {% extends "base.html" %} {% block content %}
        <h1>Django application</h1>
        {% endblock %}
        
            
        

    Step 9

    Create views.py file in mysite folder and put in there render for your home page.

    • ./src/mysite/views.py
            
                
        from django.shortcuts import render
    
        def home_view(request):
          return render(request, "pages/home.html", {})
        
            
        

    Step 10

    Then in urls.py file (also located in mysite folder) add import newly created views.py file and attach urls for paths with static files.

    • ./src/mysite/urls.py
            
                
        from django.contrib import admin
        from django.urls import path
        from django.conf import settings
        from . import views
        
        urlpatterns = [
            path('admin/', admin.site.urls),
            path('', views.home_view, name='home')
        ]
        
        if settings.DEBUG:
            from django.conf.urls.static import static
            urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
        
            
        

    Step 11

    To see if everything works, run the server:

    • Terminal
            
                    
        python manage.py runserver
        
            
        

    Installing and configuring Tailwind CSS and TW Elements

    Step 1

    To initialize an npm project, run the following command in the root directory:

    • Terminal
            
                
        npm init -y
        
            
        

    Step 2

    Install Tailwind CSS.

    • Terminal
            
                
        npm install -D tailwindcss postcss autoprefixer
        npx tailwindcss init -p
        
            
        

    File structure

    If you have followed all instructions mentioned earlier, your file structure should look like this:

          
              my-project/
              ├── node_modules/
              ├── src/        
              │   ├── mysite/
              │   │   ├── ...
              │   │   ├── settings.py
              │   │   ├── urls.py
              │   │   ├── views.py
              │   │   └── ...
              │   ├── static/
              │   |   └── input.css
              │   ├── templates/
              │   |   ├── pages/
              │   |   |   └── home.html
              │   |   └── base.html
              │   └── manage.py
              ├── venv/
              ├── package-lock.json
              ├── package.json
              ├── postcss.config.js
              └── tailwind.config.js
          
        

    Step 3

    Add the paths to all of your template files in your tailwind.config.js file.

    • tailwind.config.js
            
                
        /** @type {import('tailwindcss').Config} */
        module.exports = {
          content: [
            "./src/**/*.{html,js}",
            "./node_modules/tw-elements/js/**/*.js",
          ],
          theme: {
            extend: {},
          },
          darkMode: "class",
          plugins: [require("tw-elements/plugin.cjs")],
        };    
        
            
        

    Step 4

    Replace your own styles in input.css file with Tailwind directives.

    • ./src/static/input.css
            
                
        @import url("https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900&display=swap");
        @tailwind base;
        
        @layer base {
          html {
            @apply text-surface;
            @apply bg-white;
          }
          html.dark {
            @apply text-neutral-50;
            @apply bg-body-dark;
          }
        }
        
        @tailwind components;
        @tailwind utilities;
        
        p {
          @apply leading-[1.6];
        }      
        
            
        

    Step 5

    Install TW Elements.

    • Terminal
            
                
        npm install tw-elements
        
            
        

    Step 6

    Go to settings.py file and update section with paths for static files.

    • ./src/mysite/settings.py
            
                    
        ...
        STATICFILES_DIRS = [
          BASE_DIR / "static",
          BASE_DIR.parent / "node_modules",
        ] 
        ...
        
            
        

    Step 7

    In the head section of the base.html file, substitute the href attribute from input.css to the Tailwind CSS file named output.css, which will be generated during the Tailwind CLI build process. Furthermore, add a link to the TW Elements JS at the end of the body tag.

    • ./src/templates/base.html
            
                
        ...
        <head>
          ...
          <link href="{% static 'output.css' %}" rel="stylesheet" />
          ...
        </head>
        <body>
          ...
          <script
            src="{% static 'tw-elements/dist/js/tw-elements.umd.min.js' %}"
            type="text/javascript"></script>
        </body>
        ...
        
            
        

    Step 8

    Add the TWE components you intend to use in your project.

    • ./src/templates/pages/home.html
            
                    
        {% extends "base.html" %} {% block content %}
        <div class="mt-16 flex justify-center">
          <p class="text-lg">
            Hover the link to see the
            <a
              href="#"
              class="text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600"
              data-twe-toggle="tooltip"
              title="Hi! I'm tooltip"
              >tooltip</a
            >
          </p>
        </div>
        {% endblock %}
        
            
        

    Step 9

    Start the Tailwind CLI build process. Run the CLI tool to scan your template files for classes and build your CSS.

    • Terminal
            
                
        npx tailwindcss -i src/static/input.css -o src/static/output.css --watch
        
            
        

    And run the local server. If changes doesn't apply, try hard reload. Awesome! You're all set to dive into using TW Elements for your Django project. Have fun!

    • Terminal
            
                
        python manage.py runserver
        
            
        

    Initializing via JS

    By default all components have autoinit which means they are initialized by data attributes. But if you want to make init by JavaScript - there is also possibility to do that.

    Step 1

    Initialize components which are you intend to use.

    • ./src/templates/pages/home.html
            
                
        {% extends "base.html" %} {% block content %}
        <div class="mt-16 flex justify-center">
          <p class="text-lg">
            Hover the link to see the
            <a
              id="tooltip"
              href="#"
              class="text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600"
              title="Hi! I'm tooltip"
              >tooltip</a
            >
          </p>
        </div>
    
        <script type="module">
          const myTooltip = new twe.Tooltip(document.getElementById("tooltip"));
        </script>
        {% endblock %}
        
            
        

    Related resources

    Tutorials:

    introduction key concepts quick start vite mdb go and deploy create website about versions installation setup overview create landing page javascript tailwind css intellisense deploy repository

    Integrations :

    angular express laravel next nuxt react remix solid svelte sveltekit vue
    • Prerequisites
    • Creating a new Django application
    • Installing and configuring Tailwind CSS and TW Elements
    • Initializing via JS
    • Related resources
    Get useful tips & free resources directly to your inbox along with exclusive subscriber-only content.
    Join our mailing list now
    © 2024 Copyright: MDBootstrap.com

    Access restricted

    To view this section you must have an active PRO account

    Log in to your account or purchase an TWE subscription if you don't have one.

    Buy TWE PRO