Skip to content

In this project django is being used for backend and HTMX for frontend.

License

Notifications You must be signed in to change notification settings

wajidkorkani/Django-HTMX-Integration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 

Repository files navigation

# Django-And-HTMX

This project demonstrates a seamless integration between Django, a powerful Python web framework, and HTMX, a lightweight JavaScript framework that enables server-driven, interactive web experiences.

# Work With This Project

Prerequisites:

  • Python (version 3.6 or later recommended)
  • pip (Python package installer)
  • A code editor or IDE of your choice
  • A GitHub account (optional, but recommended for version control)

1. Initialize a virtual environment

For Linux and MacOS

pip install virtualenv
python -m venv venv
source venv/bin/activate

For Windows

pip install virtualenv
python -m venv venv
venv\Scripts\activate.bat

2. Install Requirements:

  • Install all requirements using pip:

    pip install -r requirements.txt

# Create And Setup Your Own Project

Prerequisites:

  • Python (version 3.6 or later recommended)
  • pip (Python package installer)
  • A code editor or IDE of your choice
  • A GitHub account (optional, but recommended for version control)

1. Project Setup:

  • Create a new directory for your project:

    mkdir django-htmx-project
    cd django-htmx-project
  • Initialize a virtual environment (recommended for managing project dependencies):

    For Linux and MacOS

    pip install virtualenv
    python -m venv venv
    source venv/bin/activate

    For Windows

    pip install virtualenv
    python -m venv venv
    venv\Scripts\activate.bat

2. Install Requirements:

  • Install all requirements using pip:

    pip install django
    pip install asgiref
    pip install django-htmx
    pip install sqlparse
  • Generate a new Django project:

    django-admin startproject mysite
    cd mysite

3. Create an App (Optional):

  • If you plan to have dedicated HTMX components, create a new Django app:

    python manage.py startapp myapp
  • Add the app to INSTALLED_APPS in mysite/settings.py:

    INSTALLED_APPS = [
        # ... other apps
        'myapp',
        'django_htmx',
    ]

4. Configure Static Files:

  • Add HTMX's static files to your Django project's static directory:

    mkdir mysite/static/htmx
    cp -r <path_to_htmx_static>/dist/* mysite/static/htmx/
    • Replace <path_to_htmx_static> with the actual path to HTMX's static files. You can usually find it in the node_modules/htmx.org/dist directory of your project's virtual environment.
  • Go to this link and copy the all code which will be dispaly on the browser screen and and paste it in the htmx.min.js file and htmx.min.js file should be here mysite/static/htmx/:

    https://unpkg.com/htmx.org@2.0.1/dist/htmx.min.js
  • Add these lines to STATICFILES in mysite/settings.py:

     STATIC_URL = '/static/'
     STATICFILES_DIRS = [
         os.path.join(BASE_DIR, 'static'),
     ]
  • Configure static file serving (optional for development):

     MIDDLEWARE = [
         # ... other middleware
         'django_htmx.middleware.HtmxMiddleware',
     ]

5. Create a Template with HTMX Integration:

  • Create a new template file (e.g., mysite/templates/myapp/index.html):

    <!DOCTYPE html>
    <html>
    <head>
        <title>Django-HTMX Example</title>
        <link rel="stylesheet" href="{% static 'css/style.css' %}">  <script src="{% static 'htmx/htmx.min.js' %}"></script>
    </head>
    <body>
        <h1>Django and HTMX</h1>
        <button hx-get="/data" hx-target="#data-container">Get Data</button>
        <div id="data-container"></div>
    
        <script>
            // Add any custom JavaScript logic here
        </script>
    </body>
    </html>
  • Explanation:

    • Include HTMX's JavaScript file (htmx.min.js).
    • Add HTMX attributes:
      • hx-get: Fetches data from the specified URL.
      • hx-target: Updates the target HTML element with the fetched data.
      • Explore other HTMX attributes for various interactive functionalities.

6. Create a View to Handle HTMX Requests:

  • Create a view function in your app's views.py (or mysite/views.py if not using an app):

    from django.http import JsonResponse
    
    def get_data(request):
        data = {'message': 'Hello from Django!'}  # Replace with your desired data
        return JsonResponse(data)
  • Explanation:

    • This view returns a JSON response when the button is clicked using HTMX.

Running the Project:

  1. Run migrations

About

In this project django is being used for backend and HTMX for frontend.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published