Python django (1) One of the study notes
django development steps (preparation work)
1. The current python environment python3.7 (this step is omitted, after all, it mainly talks about Django)
2. Install django
1. Enter CMD
2. Under the DOS command pip install Django I installed it under win. If it is another operating system, you can refer to (https://www.runoob.com/django/django-install.html)
#------------ --------------------
requirements.txt format is as follows:
You can also use the PIP command
pip install -r requirements.txt in batches
#---------cmd window lists all installed packages
pip freeze >requirements.txt
start django project after preparations are completed
:: Start Project
#-------------Steps --------------------------
:: Create Project TsestDjango (Project name)
django-admin startproject TsestDjango
:: enter project
cd DjangoSample view file
:: start project
python manage.py runserver
Tips python manage.py runserver 0.0.0.0:9000 The port behind it is set by yourself, where 0.0.0.0 can define your IP address or its own default
At this point, you can open http://127.0.0.1:8000/ on your webpage and you can see the following picture
The project is created Now let's create the first application:
:: Create application
python manage.py startapp ch01
:: Start the project
3. Synchronize the database
First generate the migration file through python manage.py makemigrations, as shown in the following figure in the notice 0001_initial.py file.
Next, the migration is realized through python manage.py migrate, as shown in the figure below:
:: Start the project
python manage.py runserver
Open PYCHARM Open the created project and you can see the following
Use the python manage.py runserver command to start the service, as shown in the following figure after startup:
Generally, as long as you are developing, you don’t need to turn off the service. Of course, sometimes, when your code has a serious error, you need to Shut down and restart a bit. The shortcut key to shut down the server is:
Ctrl + C
!2. Log in to the system
Visit http://127.0.0.1:8000/admin/login/, log in with the super administrator account you created (local account is pjn, password is 12345678).
After activating the admin management page of django's own server by referring to this tutorial, try to open http://127.0.0.1:8000/admin/ as described in the tutorial, and find that your browser displays the following
missing css
It can be clearly seen that although the business logic is normally expressed, CSS has been lost, and the standard black-and-white interactive environment always makes people feel uncomfortable.
I checked the error message on the command line and found the following error message:
error message
Well, it seems that we have found the problem. It turned out to be the file encoding problem called mimetypes.py. Now we open this file and add at the end of the file:
reload(sys)
sys.setdefaultencoding('gbk')
How to create your own user name and password?
Under the DOC command, ctrl+C ends the server service status
It is preferred to create a new user to log in to the Django management website, enter the manage.py directory, and use the following command to create it:
python manage.py createsuperuser
Next enter the user name:Username(leave bkank to use 'administrator'): root
and then enter the mailbox (QQemail, etc. are all available):Email address: (Enter your email account)
Enter the password (it will not be displayed when entering the password, and the length must be more than eight characters):Password:********
Password(again):********
When the two passwords are the same and exceed eight characters, it will prompt that the creation of the super user is successful:
Superuser created successfully.
Run your service again, enter the account number and password to log in successfully:
python manage.py runserver
The first application hello world
creates two directories and one file under CH01 under the first application
directory templates
static
file urls.py
Add the following code in views.py
from django.shortcuts import render
def hello_world(request):
return render(request,
'testch01/hello_word.html',
{'title': 'Hello', 'message': "Hello world! This is my first trial. [Poll's note]"} )
Add the following code to urls.py
from django.urls import path, re_path
import ch01.views
from ch01 import views
app_name = ‘testch01’
urlpatterns = [
path(‘helloworld’, ch01.views.hello_world),
]
Add the following code in apps.py
from django.apps import AppConfig
class Ch01Config(AppConfig):
name = ‘ch01’
Finally, create a subdirectory testch01 directory in the templates directory and create an html file.
The file name corresponds to the name of ch01.views.hello_world hello_world.html
The code is as follows:
Then start the service:
python manage.py runserver
Then watch your success in the browser
http://127.0.0.1:8000/ch01/helloworld
The following is their relationship diagram
Up to now, the first program has been completed. In the afternoon, I edited the second initial upload. I am sorry that there is an error. Please give me your advice.
<4> Generate a script to synchronize the database: python manage.py makemigrations
同步数据库: python manage.py migrate
注意:在开发过程中,数据库同步误操作之后,难免会遇到后面不能同步成功的情况,解决这个问题的一个简单粗暴方法是把migrations目录下
的脚本(除__init__.py之外)全部删掉,再把数据库删掉之后创建一个新的数据库,数据库同步操作再重新做一遍。
————————————————