Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| Ambdós costats versió prèvia Revisió prèvia Següent revisió | Revisió prèvia | ||
|
django_react [2026/04/10 08:29] enric_mieza_sanchez [Codi de l'aplicació ReactJS] |
django_react [2026/04/14 18:26] (actual) enric_mieza_sanchez [Django-Environ i el VCS] |
||
|---|---|---|---|
| Línia 135: | Línia 135: | ||
| from django.shortcuts import get_object_or_404 | from django.shortcuts import get_object_or_404 | ||
| from typing import List, Optional, Union, Literal | from typing import List, Optional, Union, Literal | ||
| + | import datetime | ||
| from .models import * | from .models import * | ||
| Línia 152: | Línia 153: | ||
| qs = Llibre.objects.all() | qs = Llibre.objects.all() | ||
| return qs | return qs | ||
| - | |||
| </ | </ | ||
| Línia 200: | Línia 200: | ||
| # variables a llegir de .env | # variables a llegir de .env | ||
| + | # ULL! cal elimninar les variables que hi ha al settings.py | ||
| DEBUG = env(' | DEBUG = env(' | ||
| SECRET_KEY = env(' | SECRET_KEY = env(' | ||
| Línia 207: | Línia 208: | ||
| ' | ' | ||
| } | } | ||
| + | # dominis amb autorització per a fer crides a l'API | ||
| CORS_ALLOWED_ORIGINS = env.list(" | CORS_ALLOWED_ORIGINS = env.list(" | ||
| " | " | ||
| " | " | ||
| ]) | ]) | ||
| + | # protecció CSRF per atacs de dominis creuats | ||
| + | CSRF_TRUSTED_ORIGINS = env.list(" | ||
| </ | </ | ||
| Línia 221: | Línia 225: | ||
| ALLOWED_HOSTS=*, | ALLOWED_HOSTS=*, | ||
| CORS_ALLOWED_ORIGINS=http:// | CORS_ALLOWED_ORIGINS=http:// | ||
| + | CSRF_TRUSTED_ORIGINS=http:// | ||
| </ | </ | ||
| Línia 230: | Línia 235: | ||
| Creació del projecte React dins la carpeta '' | Creació del projecte React dins la carpeta '' | ||
| + | |||
| + | Ens hem de posar a la carpeta principal del projecte Django, al mateix nivell que el '' | ||
| $ npm create vite@latest react | $ npm create vite@latest react | ||
| Línia 238: | Línia 245: | ||
| Els arxius de codi seran: | Els arxius de codi seran: | ||
| * App.jsx | * App.jsx | ||
| + | * config.js | ||
| * services/ | * services/ | ||
| * components/ | * components/ | ||
| Línia 246: | Línia 254: | ||
| * Modal.css | * Modal.css | ||
| * styles.css | * styles.css | ||
| + | * index.css | ||
| Línia 268: | Línia 277: | ||
| export default App; | export default App; | ||
| + | </ | ||
| + | |||
| + | <file javascript config.js> | ||
| + | const config = { | ||
| + | development: | ||
| + | API_URL: ' | ||
| + | DEBUG: true, | ||
| + | }, | ||
| + | production: { | ||
| + | API_URL: ' | ||
| + | DEBUG: false, | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | const env = process.env.NODE_ENV || ' | ||
| + | export default config[env]; | ||
| + | |||
| </ | </ | ||
| <file javascript services/ | <file javascript services/ | ||
| - | const API_URL = 'http:// | + | import config from '../config'; |
| + | |||
| + | const API_BASE_URL = config.API_URL; | ||
| export const getBooks = () => { | export const getBooks = () => { | ||
| console.log(' | console.log(' | ||
| - | return fetch(API_URL) | + | return fetch(API_BASE_URL+"/ |
| .then((response) => { | .then((response) => { | ||
| if (!response.ok) { | if (!response.ok) { | ||
| Línia 556: | Línia 584: | ||
| p { | p { | ||
| color: black; | color: black; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Aquest sol venir a l' | ||
| + | <file css index.css> | ||
| + | :root { | ||
| + | font-family: | ||
| + | line-height: | ||
| + | font-weight: | ||
| + | |||
| + | color-scheme: | ||
| + | color: rgba(255, 255, 255, 0.87); | ||
| + | background-color: | ||
| + | |||
| + | font-synthesis: | ||
| + | text-rendering: | ||
| + | -webkit-font-smoothing: | ||
| + | -moz-osx-font-smoothing: | ||
| + | } | ||
| + | |||
| + | a { | ||
| + | font-weight: | ||
| + | color: #646cff; | ||
| + | text-decoration: | ||
| + | } | ||
| + | a:hover { | ||
| + | color: #535bf2; | ||
| + | } | ||
| + | |||
| + | body { | ||
| + | margin: 0; | ||
| + | display: flex; | ||
| + | place-items: | ||
| + | min-width: 320px; | ||
| + | min-height: 100vh; | ||
| + | } | ||
| + | |||
| + | h1 { | ||
| + | font-size: 3.2em; | ||
| + | line-height: | ||
| + | } | ||
| + | |||
| + | button { | ||
| + | border-radius: | ||
| + | border: 1px solid transparent; | ||
| + | padding: 0.6em 1.2em; | ||
| + | font-size: 1em; | ||
| + | font-weight: | ||
| + | font-family: | ||
| + | background-color: | ||
| + | cursor: pointer; | ||
| + | transition: border-color 0.25s; | ||
| + | } | ||
| + | button: | ||
| + | border-color: | ||
| + | } | ||
| + | button: | ||
| + | button: | ||
| + | outline: 4px auto -webkit-focus-ring-color; | ||
| + | } | ||
| + | |||
| + | @media (prefers-color-scheme: | ||
| + | :root { | ||
| + | color: #213547; | ||
| + | background-color: | ||
| + | } | ||
| + | a:hover { | ||
| + | color: #747bff; | ||
| + | } | ||
| + | button { | ||
| + | background-color: | ||
| + | } | ||
| } | } | ||
| </ | </ | ||