PrayTeam / scriptured-prayer

GNU Affero General Public License v3.0
1 stars 0 forks source link

Pick Frontend Tech Stack #3

Closed Soyokaze-42 closed 5 months ago

Soyokaze-42 commented 6 months ago

Choose:

Django templates, CSS, HTML Django templates, RESTful Django API, javascript javascript, RESTful Django API

pls consider Django tutorial and Django internationalization with javascript

Soyokaze-42 commented 6 months ago

I've been looking at the rest_framework in Django. I still have a lot to learn, but it looks solid. If you want to see the usercards in the API, it would look something like what is in branch django_rest_framework.

kenancasey commented 6 months ago

Here's what ChatGPT said when I asked about different approaches to using Django and React.

There are various approaches to integrating Django (backend) with React (frontend) in a full stack web app. Two common methods are:

  1. Django REST framework with React as a separate frontend:

    • Django handles the backend and provides a RESTful API using Django REST framework.
    • React operates as a standalone frontend, communicating with the Django backend through API calls.
    • Advantages include clear separation of concerns and flexibility, allowing you to switch out the frontend if needed.
    • Development teams can work independently on the frontend and backend.
  2. Django with React as a templating engine:

    • Django renders templates that include React components.
    • React is integrated into Django templates, and the backend sends initial data to be consumed by React components.
    • This approach simplifies the development setup as you work within a single codebase.
    • Tighter integration might lead to dependencies between frontend and backend development.

The choice depends on project requirements and team preferences. The RESTful API approach is more modular and can support different frontends. Using React with Django templates is simpler but can result in a more coupled architecture.

Consider factors like team expertise, scalability, and whether you foresee the need to decouple the frontend and backend in the future when making your decision.

kenancasey commented 6 months ago

I also asked about auth:

In a Django project where React is used as a templating engine, authentication is typically handled by Django itself. Django provides a built-in authentication system that includes user authentication, user registration, and session management. Here's a basic overview of how authentication is implemented in this approach:

Authentication in Django with React as a Templating Engine:

  1. Django User Authentication:

    • Use Django's built-in authentication views and forms for user login, registration, and password reset.
    • Leverage the django.contrib.auth module for user models, authentication middleware, and decorators.
  2. Session-based Authentication:

    • Django uses session-based authentication by default. After a successful login, a session is created for the user, and the session ID is stored in a cookie.
  3. Protecting Views:

    • Use the @login_required decorator on views that require authentication. This ensures that only authenticated users can access certain parts of the application.
  4. Passing User Data to React:

    • During template rendering, you can include user-specific data in the HTML, making it accessible to React components.
  5. CSRF Protection:

    • Django provides CSRF protection out of the box. Ensure that your React forms include the CSRF token when making requests to Django views.

Authentication in Django API with Separate React Frontend:

  1. Django REST Framework (DRF) Authentication:

    • Use Django REST Framework for building APIs. DRF includes various authentication classes such as Token Authentication, Session Authentication, or JSON Web Token (JWT) Authentication.
  2. Token-based Authentication:

    • When using Token Authentication, a token is issued upon successful login. This token is sent with each subsequent request in the Authorization header.
  3. JWT-based Authentication:

    • If using JWT Authentication, a token containing user information is generated upon login. This token is signed and can be verified on the server.
  4. Stateless Authentication:

    • API authentication is typically stateless, meaning each request must include authentication credentials. There's no reliance on sessions as in traditional Django applications.
  5. Authentication Headers in React:

    • In the React frontend, you need to manage authentication headers manually, including the token or other credentials in requests to the Django API.

Comparison:

Templating Engine Approach:

  • Pros:

    • Session-based authentication is simpler to set up.
    • Easier integration with Django's built-in authentication views and forms.
    • Direct access to user data during template rendering.
  • Cons:

    • Tightly coupled frontend and backend may limit flexibility.
    • Limited use of modern authentication mechanisms like JWT.

Separate Frontend with Django API:

  • Pros:

    • Stateless authentication (Token or JWT) can be more scalable and flexible.
    • Clear separation of concerns between frontend and backend.
    • Better support for modern authentication practices.
  • Cons:

    • Additional complexity in managing authentication tokens in the frontend.
    • May require more effort to set up and configure.

The choice between these approaches depends on the project requirements, scalability needs, and the development team's familiarity with the chosen authentication methods. In modern applications, the separate frontend with a Django API approach is often preferred for its flexibility and scalability.

kenancasey commented 6 months ago

And for completeness, here's what ChatGPT said about internationalization:

Internationalization (i18n) involves designing and developing software in a way that makes it adaptable to different languages and regions. Let's compare how the two approaches to integrating Django and React handle internationalization:

1. Django with React as a Templating Engine:

  • Pros:

    • Django has robust built-in support for internationalization through the gettext module.
    • Use of Django templates allows for seamless integration of translated text directly within the HTML, making it easy to internationalize static content.
  • Cons:

    • React components might need additional handling for dynamic content and user interface elements.
    • While Django templates handle static content well, dynamic content in React components may require a different approach for translation.

2. Separate Django API with React Frontend:

  • Pros:

    • Django still provides excellent internationalization support for the backend API using the gettext module.
    • The frontend, being a separate React application, can use internationalization libraries like react-i18next or react-intl to handle translations within the React components.
    • More flexibility in managing translations on the frontend side.
  • Cons:

    • Slightly more complex setup compared to the Django templating approach, as you need to manage translations both on the Django side (for API responses) and on the React side.

Conclusion:

In both approaches, Django plays a crucial role in handling the backend, and its internationalization capabilities are still leveraged. The key difference lies in how the frontend, especially dynamic content in React components, deals with translations.

  • Django Templating Engine Approach:

    • Direct integration of translated content in Django templates simplifies the handling of static content.
    • Additional care may be needed for dynamic content within React components.
  • Separate Django API with React Frontend:

    • More flexibility for handling translations within React components.
    • Slightly more effort needed to synchronize translations between the Django backend and the React frontend.

When it comes to internationalization, both approaches are viable, and the choice may depend on other factors such as the overall project structure, team expertise, and the desired level of flexibility in handling translations on the frontend. The separate frontend with a Django API approach provides more control and flexibility for handling translations within React components but comes with a bit more complexity in terms of coordination between backend and frontend translations.

Soyokaze-42 commented 6 months ago

@kenancasey Did you fact-check ChatGPT? That is a lot of information for us to sort through if not.

Soyokaze-42 commented 6 months ago

ChatGPT got the general concept pretty right, but not internationalization. Take a look at the docs here: https://docs.djangoproject.com/en/4.2/topics/i18n/translation/#internationalization-in-javascript-code

If we would use this with templates, the coupling would let django see the translation strings and Kelly's process would be the same for text in the front and back ends. With the rest_framework, I bet we could use the same Django generated library for translations but would have to automate pulling the translation strings out of the javascript and into Django for translation or do internationalization independently.

One of the places Django uses for selecting the user language is in the URL (en in the demo). We would also have to use another way to choose the language if we decouple completely. I believe there is a way to store it with the UserProfile or include it in a cookie, but that is something we will have to figure out.

Soyokaze-42 commented 6 months ago

Django Templates with javascript hashed out 12/19 :)

kenancasey commented 5 months ago

Changed to react spa frontend with Django backend.