Django is an exceptional choice for building a full-stack Web App in 2024. It stands apart from typical hype cycles and is known for its maturity and stability. Combined with tailwindcss it truly is one of our favourite frameworks.
The django-app template gives you a full-stack Web App built with Django and Postgres
Create your codebase using the django-app template pre-configured with Django and PostgreSQL
phi ws create -t django-app -n django-app
This will create a folder named django-app with the following structure:
django-app # root directory for your django-app├── app # directory for the Jjango project├── nginx # directory for nginx used in production for static files├── manage.py # django-admin file├── dev.Dockerfile # Dockerfile for the dev application├── prd.Dockerfile # Dockerfile for the production application├── pyproject.toml # python project definition├── requirements.txt # python dependencies generated by pyproject.toml├── scripts # directory for helper scripts├── tests # directory for unit tests└── workspace # phidata workspace directory ├── dev_resources.py # dev resources running locally ├── prd_resources.py # production resources running on AWS ├── secrets # directory for storing secrets └── settings.py # phidata workspace settings
Your codebase comes with a pre-configured Django application connected to a Postgres database. Run it using:
phi ws up
Press Enter to confirm and give a few minutes for the image to download (only the first time). Verify container status and view logs on the docker dashboard.
The Django Tutorials are a great place to learn about Django, play around and update your Web App to your use case. Here's how to create a simple chat application:
from django.shortcuts import render, redirectfrom phi.agent import Agentdef index(request): try: # Create a agent agent = Agent() if 'messages' not in request.session: request.session['messages'] = [] if request.method == 'POST': prompt = request.POST.get('prompt') # Add the prompt to the messages request.session['messages'].append({"role": "user", "content": prompt}) # Set the session as modified request.session.modified = True # Create a response response = agent.run(prompt, stream=False) # Append the response to the messages request.session['messages'].append({"role": "agent", "content": response}) # Set the session as modified request.session.modified = True # Redirect to the home page context = { 'messages': request.session['messages'], 'prompt': '', } return render(request, 'chat/index.html', context) else: context = { 'messages': request.session['messages'], 'prompt': '', } return render(request, 'chat/index.html', context) except Exception as e: print(e) return redirect('index')def new_chat(request): # -*- Clears the session messages and redirects to the home page -*- request.session.pop('agent', None) request.session.pop('messages', None) return redirect('index')