Django study notes (2)-create APP

Create application

The previously opened terminal window runs runserver, then opens a terminal window, and switches to the directory where manage.py is located. Activate the virtual environment and execute commands startapp;

twr@twr-911K:~/learning_log$ source ll_env/bin/activate
(ll_env) twr@twr-911K:~/learning_log$ python manage.py startapp learning_logs
(ll_env) twr@twr-911K:~/learning_log$ ls
db.sqlite3  learning_log  learning_logs  ll_env  manage.py
(ll_env) twr@twr-911K:~/learning_log$ ls learning_logs
admin.py  apps.py  __init__.py  migrations  models.py  tests.py  views.py

Define the model

Modules models are imported in models.py, we can define our own models

Activation model

Open settings.py and modify INSTALLED_APPS as follows:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    #myapps
    'learning_logs',
]

Let Django modify the database and execute the following command in a terminal window:

(ll_env) twr@twr-911K:~/learning_log$ python manage.py makemigrations learning_logs
Migrations for 'learning_logs':
  learning_logs/migrations/0001_initial.py
    - Create model Topic

Migration modification:

(ll_env) twr@twr-911K:~/learning_log$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, learning_logs, sessions
Running migrations:
  Applying learning_logs.0001_initial... OK

Django management website

Create Super User

Enter the following command in the terminal:

python manage.py createsuperuser

Register the model with the management website

Modify admin.py

from django.contrib import admin

# Register your models here.
from learning_logs.models import Topic

admin.site.register(Topic)

Visit http: // localhost: 8000 / admin /
Insert picture description hereInsert picture description here

Create a webpage

Map url

The urls.py file in learning_log:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('learning_logs.urls',namespace='learning_logs')),
]

The admin.site.urls module defines all the URLs that can be requested in the management website.
The following line distinguishes the URL of learning_logs from the URLs in the project.
Create a new url.py in the learning_logs folder:

from django.conf.urls import url
from . import views

urlpatterns = [
        url(r'^$', views.index, name='index'),
]

app_name = 'learning_logs'

from django.conf.urls import url: Import function url, used to map url to view
from . import views: Import module views, where the period will import the view into the folder where the current urls.py module is located.
urlpatterns: List, contains web pages that can be requested in the application learning_logs. Inside is a call to the function url (), which has three arguments. The first one is a regular expression. Defines the Django searchable mode (find URLs that have nothing between the beginning and the end). The second argument specifies the view function to be called. When the requested url matches the aforementioned regular expression, Django will call views.index. The third argument specifies the name of this url pattern as index.

Compose view

from django.shortcuts import render

# Create your views here.
def index(request):
    return render(request, 'learning_logs/index.html')

render () provides two actual parameters: the original request object and a template that can be used to create a web page.

Published 28 original articles · won praise 2 · Views 3259

Guess you like

Origin blog.csdn.net/Maestro_T/article/details/103895499