python__django_tutorials/docs/ch2-overview.md

59 lines
1.4 KiB
Markdown

# 2. Overview of templates
The demo project will be a ticketing app.
## Setting up
In this example we will be creating views in the project,
instead of using apps, in `ticketing/ticketing/views.py`,
so all files are in `ticketing/ticketing` by default
- activate environment: `workon <venv>`
- install Django, update everything: `pip install -U Django==3.2`
- create project: `django-admin startproject ticketing`
- add `views.py` and create a view in it:
````python
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world")
````
- route that controller to the home page in `urls.py`
```python
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name="index"),
]
```
- enable the app in the project in `settings.py#INSTALLED_APPS`
```python
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ticketing,'
]
```
## Best practices
- Do not place the templates directly in `templates`,
but add a subdirectory named by the app, as in `templates/ticketing`.
This helps when combining apps in projects
- Create directories within that "templates/<app>" directory:
- Place `/edit` and `/view` templates within the `/ticketing` folder.