Skip to content

Commit 5ffd399

Browse files
Merge pull request #4 from RustamovAkrom/main
Main
2 parents b98610c + 84062e3 commit 5ffd399

File tree

10 files changed

+112
-81
lines changed

10 files changed

+112
-81
lines changed

.env-example

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
#### Django secret key ####
4+
SECRET_KEY=SECRET_KEY
5+
6+
#### Django debug mode default True ####
7+
DEBUG=True
8+
9+
#### Django allowed hosts default localhost, 127.0.0.1 ####
10+
ALLOWED_HOSTS=localhost,127.0.0.1
11+
12+
#### JWT (RS256) private, public keys default ####
13+
PRIVATE_KEY_PATH=security_settings/private_key.pem
14+
PUBLIC_KEY_PATH=security_settings/public_key.pem
15+
16+
#### Django database environs sqlite, postgres ####
17+
DATABASE_ENVIRON=sqlite
18+
19+
#### PostgreSQL configurations ####
20+
POSTGRES_NAME=POSTGRES_NAME
21+
POSTGRES_USER=POSTGRES_USER
22+
POSTGRES_PASSWORD=POSTGRES_PASSWORD
23+
POSTGRES_HOST=localhost
24+
POSTGRES_PORT=5432

README.md

Lines changed: 11 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,16 @@
1-
# Django Blog App with HTMX
1+
# Django Blog app adevanced project
22

33
![logo](/logo.png)
44

5-
A modern and dynamic blog application built using **Django** and **HTMX**. This project demonstrates the integration of HTMX for building interactive and seamless user experiences without relying heavily on JavaScript frameworks.
5+
## Technologies:
6+
- Django
7+
- Django REST
8+
- Django Simple JWT
9+
- Authentication for JWT (RS256)
10+
- HTML, CSS, JavaScript, Bootstrap
611

7-
## Features
12+
## Databases:
13+
+ PostgreSQL
14+
+ SQLite3
815

9-
- **HTMX Integration**: Enhance the user experience with partial page updates and AJAX-like behavior without writing JavaScript.
10-
- **Post Creation and Editing**: Add, edit, and delete blog posts dynamically with inline forms.
11-
- **Comments System**: Add, delete, and update comments without reloading the page.
12-
- **User Authentication**: Register, log in, and log out functionality for users.
13-
- **Responsive Design**: The UI is responsive and mobile-friendly.
14-
15-
## Technologies Used
16-
17-
- **Django**: Backend framework.
18-
- **HTMX**: Frontend interactivity for handling requests and updates.
19-
- **Bootstrap**: For responsive and modern UI design.
20-
- **SQLite**: Default database for development.
21-
22-
## Getting Started
23-
24-
### Prerequisites
25-
26-
Ensure you have the following installed:
27-
28-
- Python 3.x
29-
- Django 4.x
30-
- HTMX
31-
32-
### Installation
33-
34-
1. Clone the repository:
35-
36-
```bash
37-
git clone https://github.com/yourusername/django-blog-htmx.git
38-
cd django-blog-htmx
39-
```
40-
41-
2. Create a virtual environment and activate it:
42-
43-
```bash
44-
python -m venv env
45-
source env/bin/activate # On Windows: `env\Scripts\activate`
46-
```
47-
48-
3. Install the required dependencies:
49-
50-
```bash
51-
pip install -r requirements.txt
52-
```
53-
54-
4. Apply the migrations:
55-
56-
```bash
57-
python manage.py migrate
58-
```
59-
60-
5. Run the development server:
61-
62-
```bash
63-
python manage.py runserver
64-
```
65-
66-
6. Open the app in your browser:
67-
68-
```
69-
http://127.0.0.1:8000/
70-
```
71-
72-
### Usage
73-
74-
- Create a new post, edit existing posts, and add comments to any post. All updates are handled smoothly using HTMX for a better user experience.
75-
<<<<<<< HEAD
76-
77-
## Project Structure
78-
=======
79-
>>>>>>> master
16+
[Documentations](docs/index.md)

apps/users/signals.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
from django.db.models.signals import post_save
2+
from django.http import JsonResponse
23
from django.dispatch import receiver
34
from .models import User, UserProfile
45

6+
from rest_framework_simplejwt.tokens import RefreshToken
7+
from allauth.account.signals import user_logged_in
8+
59

610
@receiver(post_save, sender=User)
711
def create_user_profile(sender, instance, created, **kwargs):
812
if created:
913
print(f"Creating UserProfile to User({instance})")
1014

1115
UserProfile.objects.create(user=instance)
16+
17+
@receiver(user_logged_in)
18+
def generate_jwt_token(sender, request, user, **kwargs):
19+
refresh = RefreshToken.for_user(user)
20+
access_token = str(refresh.access_token)
21+
22+
data = {
23+
"refresh": str(refresh),
24+
"access": access_token,
25+
}
26+
27+
request._dont_enforce_csrf_checks = True
28+
response = JsonResponse(data)
29+
response.status_code = 200
30+
return response

core/settings.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
ROOT_URLCONF = "core.urls"
3434

3535

36+
TEMPLATES_DIRS = ["templates"]
37+
3638
TEMPLATES = [
3739
{
3840
"BACKEND": "django.template.backends.django.DjangoTemplates",
39-
"DIRS": ["templates"],
41+
"DIRS": TEMPLATES_DIRS,
4042
"APP_DIRS": True,
4143
"OPTIONS": {
4244
"context_processors": [
@@ -52,12 +54,25 @@
5254

5355
WSGI_APPLICATION = "core.wsgi.application"
5456

55-
DATABASES = {
56-
"default": {
57-
"ENGINE": "django.db.backends.sqlite3",
58-
"NAME": BASE_DIR / "db.sqlite3",
57+
if os.getenv("DATABASE_ENVIRON") == "postgres":
58+
DATABASES = {
59+
"default": {
60+
"ENGINE": "django.db.backends.postgresql",
61+
"NAME": str(os.getenv("POSTGRES_NAME")),
62+
"USER": str(os.getenv("POSTGRES_USER")),
63+
"PASSWORD": str(os.getenv("POSTGRES_PASSWORD")),
64+
"HOST": str(os.getenv("POSTGRES_HOST")),
65+
"PORT": int(os.getenv("POSTGRES_PORT"))
66+
}
67+
}
68+
else:
69+
DATABASES = {
70+
"default": {
71+
"ENGINE": "django.db.backends.sqlite3",
72+
"NAME": BASE_DIR / "db.sqlite3",
73+
}
5974
}
60-
}
75+
6176

6277
AUTH_PASSWORD_VALIDATORS = [
6378
{
@@ -95,4 +110,3 @@
95110
AUTH_USER_MODEL = "users.User"
96111

97112
SITE_ID = 1
98-

core/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
{"sitemaps": sitemaps},
2626
name="django.contrib.sitemaps.views.sitemap",
2727
),
28+
path('accounts/', include("allauth.urls")),
2829
]
2930

3031
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

db.sqlite3

100 KB
Binary file not shown.

docs/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# My Blog app Documentation
2+
3+
Welcome to the project documentation **My Blog App**. Here you will find all the necessary instructions for installation, use and development.
4+
5+
- [Installation](installation.md )
6+
- [Usage](usage.md )
7+
- [API Reference](api_reference.md )
8+
- [Participation](contributing.md )

docs/installation.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Installation
2+
3+
1. Clone the repository:
4+
``bash
5+
git clone https://github.com/RustamovAkrom/Blog-2.git
6+
cd my-blog-app
7+
```
8+
9+
2. Install the dependencies:
10+
``bash
11+
pip install -r requirements.txt
12+
```
13+
14+
3. Apply migrations and start the server:
15+
``bash
16+
python manage.py migrate
17+
python manage.py runserver
18+
```
19+
20+
Now the project is ready to work!

docs/usage.md

Whitespace-only changes.

mkdocs.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
site_name: My Blog App Documentation
2+
nav:
3+
- Index: index.md
4+
- Installation: installation.md
5+
- Usage: usage.md
6+
- API: api_reference.md
7+
- Contributing: contributing.md
8+
theme: readthedocs

0 commit comments

Comments
 (0)