--- tags: Worshop --- # Starting a Django project 1. Install Django ``` bash conda install django python -m django --version ``` 2. Create a project ``` bash mkdir project_folder cd project_folder django-admin startproject travelclean_gui ``` Create a secret file: ``` bash ``` You need to redo this step when cloning the repository. This creates a skeleton folder structure. * The main settings are stored in a folder called "travelclean_gui". * settings.py * urls.py * asgi.py * wsgi.py * manage.py does most command line management 3. Start the development server ``` bash python3 manage.py runserver ``` This is always useful during development 4. Create a new app ``` bash python manage.py startapp cars ``` This creates a folder named "cars". The folder contains * models.py: * Models map tables in the database to Python classes * views.py: * Urls are mapped to views * Views return html, which is sent to the client * urls.py * mapping of urls to views (can be done in travelclean_gui/urls.py) * admin.py * for managing the admin interface * tests.py * test for the models, views, forms * apps.py * migrations * managed by Django Add the new app to "travelclean_gui/settings.py": ```python INSTALLED_APPS = [ 'polls.apps.PollsConfig', ... 'cars', ] ``` 5. Add a superuser ```bash python manage.py createsuperuser ``` The superuser has access to the admin pages. Check out `http://0.0.0.0:8000/admin`. 6. Add a model (data in the database) cars/models.py ```python from django.db import models class Camera(models.Model): # Columns in the database table are members in the class description = models.CharField(max_length=200) ip = models.GenericIPAddressField(max_length=50) ``` You need to update the database to reflect the new model: ```bash python manage.py makemigrations python manage.py migrate ``` This will add the table into the database. It does not contain any data, since we didn't create a camera yet. The easiest way to change the data is to add the model to admin: cars/admin.py ```python from django.contrib import admin from .models import Camera admin.site.register(Camera) ``` 7. Add a URL and view travelclean_gui/urls.py: ```python from django.contrib import admin from django.urls import include, path urlpatterns = [ path('', include('cars.urls')), path('admin/', admin.site.urls), ] ``` cars/urls.py: ```python from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ] ``` cars/views.py ```python from django.http import HttpResponse def index(request): return HttpResponse("Hello!") ``` * This view just returns text, but it's general python code, so it could be anything more complicated. * Django has a powerful templating system, that makes this a lot easier. 8. Add a view to interact with the model cars/urls.py ```python from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), path('cameralist', views.CameraListView.as_view(), name='camera_list'), path('camera/<pk>', views.CameraDetailView.as_view(), name='camera'), ] ``` cars/views.py ```python from django.shortcuts import render from django.http import HttpResponse from django.views import generic from .models import Camera # Create your views here. def index(request): return HttpResponse("Hello!") class CameraListView(generic.ListView): model = Camera template_name = 'camera_list.html' class CameraDetailView(generic.DetailView): model = Camera ``` cars/templates/cars/camera_list.html ```html <html> <body> {% block content %} <h1>Camera List</h1> {% if camera_list %} <ul> {% for camera in camera_list %} <li> {{ camera.id }}: {{camera.description}} </li> {% endfor %} </ul> {% else %} <p>No cameras found</p> {% endif %} {% endblock %} </body> </html> ``` cars/templates/cars/camera_detail.html ```html <html> <body> {% block content %} <h1>Camera</h1> {{ camera.id }}: {{camera.description}} : {{camera.ip}} {% endblock %} </body> </html> ``` * There are a number of these generic views, see https://docs.djangoproject.com/en/3.2/topics/class-based-views/generic-display/ 9. Add a form for changing the data * In fact a view can change the data on post, but forms allow you to submit more structured data