程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Configuring docker, GIT environment and project creation for acwing Django framework course

編輯:Python

Better reading experience

Python3 Basics

Python3 official Tutorial

To configure Docker,git Environment and project creation

cd /var/lib/acwing/docker/images
scp django_lesson_1_0.tar ali:~/
# Log in to your own server 
ssh ali
# Load the image 
docker load -i django_lesson_1_0.tar
# utilize django_lesson Image creation django_server Containers 
# take 20000 Port and 8000 The port is mapped to the port of the container 22 Port and 8000 port 
#22 The port is used for SSH Connect ,8000 The port is used to debug the project 
docker run -p 20000:22 -p 8000:8000 --name django_server -itd django_lesson:1.0
# Into the container 
docker attach django_server
# establish acs user 
adduser acs
# add to sudo jurisdiction 
usermod -aG sudo acs
# modify root Account password 
passwd root
# Suspend container , Restart container 
<ctrl>-p,<ctrl>-q
docker restart django_server

To configure ssh Connection alias

cd .ssh
vim config

Add the following

Host ali_dj
HostName xxx.xx.xx.xxx
User acs
Port 20000
ssh-copy-id ali_dj

Can be realized ssh No secret connection docker Containers

# Transfer the configuration file to ali_dj On 
scp .vimrc .tmux.conf ali_dj:~/
# Get into acs user 
ssh ali_dj
# Use django-admin Create project 
[email protected]:~$ django-admin --version
3.2.8
[email protected]:~$ django-admin startproject acapp
[email protected]:~$ tree .
.
└── acapp
├── acapp
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
2 directories, 6 files

To configure git Environmental Science

# Generate the key 
ssh-keygen
# Copy the public key to gitlab On 
cat .ssh/id_rsa.pub
# Global configuration 
cd acapp
git init
git config --global user.name "username"
git config --global user.email "useremail"
git remote add origin [email protected]:Misaka_9982/acapp.git
git add .
git commit -m "Initial commit"
git push --set-upstream origin master

Set up Django To configure

Start project

python3 manage.py runserver 0.0.0.0:8000

Use ag Command found ALLOWED_HOSTS

[email protected]:~$ ag ALLOWED_HOSTS
acapp/acapp/settings.py
28:ALLOWED_HOSTS = []
# Enter the file , Put the server ip Add address to 
ALLOWED_HOSTS = ["xxx.xx.xx.xxx"]

The following interface appears

The following prompt appears on the command line :

Follow the instructions

python3 manage.py migrate


In the use of git The process of discovery will python The compiled file of .pyc Files have been added to git In the cache of , Pictured

git add .
git status


Use the command to delete the compiled file

git rm --cache *.pyc

To write .gitignore file , These compiled files will be ignored in the future

~/acapp$ vim .gitignore
# Format :**/ file name 
**/__pycache__
*.swp

Create administrator user

stay ip:port Followed by /admin You can see the interface shown in the following figure

# Create a superuser 
python3 manage.py createsuperuser


The following interface can be seen when logging in the website

Create a Django app

python3 manage.py startapp game
tree .


game The functions of the files in the directory

  1. urls.py:Django Route on urls.py To configure ,urls.py Each configuration in corresponds to the corresponding processing method .
    Routing is simply based on user requests URL Link to determine the corresponding handler , And return the processing result , That is to say URL And Django Establish a mapping relationship between the views of .
  2. views.py: A view function , Short for view , It's a simple one Python function , It accepts Web Request and return Web Respond to .
    The response can be a HTML page 、 One 404 Error page 、 redirect page 、XML file 、 Or a picture …
    No matter what logic the view itself contains , All return responses . Code can be written anywhere , As long as Python Below directory , It is usually placed at the end of the project views.py In file .
    Each view function is responsible for returning a HttpResponse object , Object contains the generated response .
    There are two important objects in the view layer : Request object (request) With the response object (HttpResponse).
  3. templates Catalog : management html file
  4. models Catalog : Manage database data

Edit view interface

game.views:

from django.http import HttpResponse
def index(request):
line1 = '<h1 > First page </h1>'
return HttpResponse(line1)

game.urls:

from django.urls import path
from game.views import index
urlpatterns = [
path("", index, name="index"),
]

acapp.urls:

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('game.urls')),
path('admin/', admin.site.urls),
]

Route redirection path :
browser URL URL input -> acapp.urls -> game.urls-> game.views.index -> views The page display

Open the web page and input the corresponding address to get the following page


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved