Django basics essays

Foreword

I am a little rookie, blog in just to record some of my learning process, but also to write down later easier to find, will also add or change content, it does not guarantee the accuracy of content, there is the wrong place to welcome you point it out . Using python 3.6.7, django 2.1.7, with the Anaconda installation environment, IDE using pycharm Pro.

Create a Django project

In the command line to create the project: django-admin startproject HelloWorld
the HelloWorld is the project name, also in the name of the computer project folder of
the project directory:
the HelloWorld: container project.
manage.py: a useful command-line tool that allows you to interact in various ways with the Django project.
The HelloWorld / the init .py: an empty file that tells Python that directory is a Python package.
HelloWorld / settings.py: Set the Django project / configuration.
HelloWorld / urls.py: URL declare the Django project; a website driven by Django "directory."
HelloWorld / wsgi.py: a WSGI-compliant Web server entry in order to run your project.

Run the project: python manage.py runserver,
this is the default with the address and port number Here Insert Picture Description
in the browser input 127.0.0.1:8000, if it is running pycharm where you can click on the address Successful operation
and port number 8000, which can be amended, but be careful that if your computer is installed "cool dog Music" and songs you are running, then run django have to change a port number, because cool dog will occupy the port number 8000, which is a very ignorant thing to force, if did not report the port is occupied, that is good.
Switch port:python manage.py runserver 127.0.0.1:9000

Creating applications

Some people may not quite understand what application (app), you put it into a different understanding on the phone app, each doing different things, they can call each other each other, just as when logged in to be called QQ and micro-channel Like, the whole project is equivalent to your cell phone, hold their inclusion.
Creating app: python manage.py startapp app
multiple executions will generate more app, sometimes to python3 manage.py ..., exactly when I use is not very clear. After executing the command line will find that nothing had happened, do not worry, you are opening the project you will find a man named app folder, which is the application you created.

After you've created the application will go to settings.py file register, you have an app to tell the project can be used as follows
Here Insert Picture Description
others are django comes, just like the phone system comes with software factory the same.

routing

Here Insert Picture Description
Routing is like a line, tell the front which direction to go, what page of the display, from App import viewsmeaning that views.py import files from the application's application, called APP, so as to call in the following views, which is hello in views of views.hello .py file view function (interface)

View function

Here Insert Picture Description
This one is to deal with local data, hello is before views.hello of hello, name should be the same! Then return to the page content called a "double-click 666" of

Project run

Here Insert Picture Description
This is pycharm in the Terminal window to run, the result represents a successful operation.
Here Insert Picture Description
Address written is because there is such a route named:
Here Insert Picture Description
Note: being on first wrote this, a little rush, the future will add more points.

Released two original articles · won praise 2 · views 81

Guess you like

Origin blog.csdn.net/weixin_45067618/article/details/104064073