Skip to content

Commit 7f5bc61

Browse files
author
grischan
committed
Add Additional Django Htmx Example
1 parent 3addc69 commit 7f5bc61

File tree

23 files changed

+501
-0
lines changed

23 files changed

+501
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Examples with `_codec` show how to use a custom codec. Examples with `_import_ho
1010
- `custom_components` - Shows how you can use custom components
1111
- `props` - Shows some advanced props usage
1212
- `custom_elements` - Shows how you can use [custom HTML elements](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements)
13+
- 'django_htmx_pyjsx' - How to use pyjsx togther with Htmx and Django
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Todo Webapp
2+
3+
A simple todo webapp built with Django, HTMX, and PyJSX.
4+
5+
## Features
6+
7+
- Create new todos
8+
- Display todos dynamically using HTMX
9+
10+
## Installation
11+
12+
1. Install dependencies using Poetry:
13+
```
14+
poetry install
15+
```
16+
17+
2. Run migrations:
18+
```
19+
poetry run python manage.py migrate
20+
```
21+
22+
3. Start the development server:
23+
```
24+
poetry run python manage.py runserver
25+
```
26+
27+
## Usage
28+
29+
Visit `http://localhost:8000` to access the todo app.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
# Set up PyJSX before anything else
10+
import pyjsx.auto_setup
11+
12+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "todo_project.settings")
13+
try:
14+
from django.core.management import execute_from_command_line
15+
except ImportError as exc:
16+
raise ImportError(
17+
"Couldn't import Django. Are you sure it's installed and "
18+
"available on your PYTHONPATH environment variable? Did you "
19+
"forget to activate a virtual environment?"
20+
) from exc
21+
execute_from_command_line(sys.argv)
22+
23+
24+
if __name__ == "__main__":
25+
main()

examples/django_htmx_pyjsx_todo/poetry.lock

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[tool.poetry]
2+
name = "todo-app"
3+
version = "0.1.0"
4+
description = "A simple todo webapp with Django, HTMX, and PyJSX"
5+
authors = ["Your Name <[email protected]>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.12"
10+
django = "^5.0"
11+
python_jsx = {git = "https://github.com/tomasr8/pyjsx.git", python = ">=3.12,<=3.14"}
12+
django-htmx = "^1.17.0"
13+
14+
[build-system]
15+
requires = ["poetry-core"]
16+
build-backend = "poetry.core.masonry.api"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{% load static %}
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Todo App</title>
8+
<script src="https://unpkg.com/[email protected]"></script>
9+
<script src="https://cdn.tailwindcss.com"></script>
10+
</head>
11+
<body class="bg-gray-100 min-h-screen flex flex-col">
12+
{% include "header.html" %}
13+
<main class="flex-grow">
14+
{% block content %}
15+
{% endblock %}
16+
</main>
17+
{% include "footer.html" %}
18+
</body>
19+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<footer class="bg-gray-100 border-t border-gray-200 py-4 mt-8">
2+
<div class="container mx-auto px-4 text-center text-gray-600">
3+
<p>&copy; 2023 Todo App. All rights reserved.</p>
4+
</div>
5+
</footer>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<header class="bg-gradient-to-r from-blue-500 to-indigo-600 text-white shadow-md h-20">
2+
</header>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% extends "base.html" %}
2+
{% load static %}
3+
{% block content %}
4+
<div class="container mx-auto p-4">
5+
<h1 class="text-3xl font-bold mb-4 text-center text-gray-800">Todo List</h1>
6+
7+
<!-- Form to create new todo -->
8+
<form hx-post="{% url 'create_todo' %}" hx-target="#todo-list" hx-swap="innerHTML" class="mb-4">
9+
{% csrf_token %}
10+
<div class="flex">
11+
<input type="text" name="title" placeholder="Enter a new todo"
12+
class="flex-grow p-3 border border-gray-300 rounded-l-lg focus:outline-none focus:ring-2 focus:ring-blue-500" required>
13+
<button type="submit"
14+
class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-3 px-6 rounded-r-lg transition duration-300">
15+
Add Todo
16+
</button>
17+
</div>
18+
</form>
19+
20+
<!-- Todo list Initial rendering only-->
21+
<div id="todo-list">
22+
{{ todos_list|safe }}
23+
</div>
24+
</div>
25+
{% endblock %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import pyjsx.auto_setup

0 commit comments

Comments
 (0)