Install Django
conda install django
python -m django --version
Create a project
mkdir project_folder
cd project_folder
django-admin startproject travelclean_gui
Create a secret file:
You need to redo this step when cloning the repository.
This creates a skeleton folder structure.
Start the development server
python3 manage.py runserver
This is always useful during development
Create a new app
python manage.py startapp cars
This creates a folder named "cars". The folder contains
Add the new app to "travelclean_gui/settings.py":
INSTALLED_APPS = [
'polls.apps.PollsConfig',
...
'cars',
]
Add a superuser
python manage.py createsuperuser
The superuser has access to the admin pages. Check out http://0.0.0.0:8000/admin
.
Add a model (data in the database)
cars/models.py
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:
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
from django.contrib import admin
from .models import Camera
admin.site.register(Camera)
Add a URL and view
travelclean_gui/urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('cars.urls')),
path('admin/', admin.site.urls),
]
cars/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
cars/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello!")
Add a view to interact with the model
cars/urls.py
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
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>
<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>
<body>
{% block content %}
<h1>Camera</h1>
{{ camera.id }}: {{camera.description}} : {{camera.ip}}
{% endblock %}
</body>
</html>
Add a form for changing the data
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing