如何启动django服务器?

Today's Chief CTO Note to share with you how to start the django server, if it happens to solve the problem you are facing now, don't forget to pay attention to this site, start now!

How to restart the server with django

If you are using runserver to run the program,

You can directly Ctrl+c, turn it off and run python manager.py runserver to restart.

Or you can turn on the debug parameter in the settings.py so that if you modify the Python code, it will automatically restart the service.

If your problem is solved, please adopt!

If not, keep asking!

How to deploy a Django project on a server and have it running in the background all the time

The other day, my teacher asked me to put a Django project (crawler web) on the campus intranet, but I wanted to try it out with my own server first. I happened to buy a server on Digital Ocean to run SS scripts, and usually the server has been useless, so I will experiment with it.

Without further ado, the first step is to upload the Django file to the server via WinSCP software.

Install the environment Django needs and the Python third-party libraries I need in the server.

Once all the above steps are done, there is one more step to do, which is a pitfall that I experienced . Open settings.py in the Django file directory and change ALLOWED_HOSTS=[] to ALLOWED_HOSTS=["*"].

Open the directory in the server to where manage.py is located and enter the command:

python3 manage.py runserver 0.0.0.0:8000

Then press Enter and enter in the browser: The server IP address: 8000, you're done!

Attention:

1) python3 is not specific, it is specified according to the environment required by your Django project.

2. 8000 is the port number and can be modified.

If you want your Django project to run all the time, after closing the terminal, you need to run the following command, nohup command, command is the python3 manage.py runserver 0.0.0.0:8000 mentioned above.

如何启动django服务器?  第1张

How to start Django Runserver locally with a script

Django is a web development framework that can be used to develop web projects, and the web needs servers to run, such as the commonly used nginx, apache, uwsgi, etc., these servers are only responsible for running programs (projects written by django), user requests will be sent on the server, and then the server requests django, and django returns the corresponding results to the web

Python 3.8 installation configuration Django environment (Part I)

Python 3.8 installation configuration Django environment (Part I)

1. CMD view the python version

2. CMD to view the pip version

3, pip installs Django, default Django3

Upgrade pip

4) Check out the Django version

5. Install MySQL

6. Check the MySQL version

7. Create the Django folder and enter,

8) Start a Django project

9. Run the server and test the Django project

10. Use the address to access the project

Django Source Code Reading (1) Project build and start

To be honest, I haven't appreciated Django so far. In my mind, it's not a very elaborate design. Just a "mature scheme" stacked up of features. But the rise of everything is the choice of the times. No matter how much you don't like it, but it's needed. Hopefully, one day, Python will have more and richer mature solutions, and will no longer be criticized for performance and maintainability. (End of)

Take the essence and remove the dross, the advantage of Django is convenience, and the purpose of our source code reading this time is to explore the essence of its convenience. It is planned that this source code reading will not be refined to every place, but will be interpreted in general terms of function.

django-admin startproject HelloWorld generates django projects, and the command line is exe format.

manage.py Leave the arguments to the command line for parsing.

execute_from_command_line() creates a management class with command-line arguments. Then run his execute().

If reload is set, it will be check_errors before starting.

check_errors() is a closure, so the above ends with (django.setup)().

Look directly at the last sentence settings. INSTALLED_APPS 。 Grab the app from Settings

Note that this setting is not yet a settings.py in our project. Instead, an object is located __init__.py Django\\Conf\\

This is a lazy loading wrapper of the Settings class that does not start initializing until the __getattr__ value. The value is then taken from the instance of the Settings class. And will say that the value is assigned to its own __dict__ (next time it will be found directly on itself, because __getattr__ has a lower priority)

To facilitate debugging, let's write a run.py directly. No command line approach.

Create a run.py under the project to simulate the runserver command

Debug grab setting_module

Go back to the last sentence in setup() apps.populate(settings. INSTALLED_APPS)

Start looking at apps.populate()

Let's start with this paragraph

These apps will eventually be encapsulated as AppConfig. It is loaded into the self.app_configs dictionary

Then, call the import_models() and ready() methods of each appConfig separately.

The loading part of the app is generally like this

To facilitate debugging, let's rephrase the last sentence

The type of res, is Command django.contrib.staticfiles.management.commands.runserver.Command object at 0x00000101ED5163A0

The focus is on the second sentence, let's skip to the run_from_argv() method, where the parameters are manipulated a bit.

Clicking the handle here with pycharm will enter the method of the base class and will not get the correct direction. In fact, the subclass Commond overrides this method.

There are two cases here, if it is reload overloading, the inner_run() will be executed directly, and the project startup needs to execute other logic first.

When a Django project starts, it actually starts twice, and if we set a print in the project entry (manage.py), we will find that it is printed twice.

On the first boot, the DJANGO_AUTORELOAD_ENV is None and cannot enter the startup logic. will enter restart_with_reloader().

Here the DJANGO_AUTORELOAD_ENV is set to True and then rebooted.

The second time, you can enter the startup logic.

A Django main thread is created here that passes in inner_run().

This thread then creates a polling daemon through reloader.run(django_main_thread).

Let's look next at Django's main thread inner_run().

When we see WSGI, the startup logic that Django is responsible for, ends there. The rest of the work is left to the WSGI server

This is equivalent to what we said earlier in FastAPI, handing over the FastAPI app to the Asgi server. (Asgi was also proposed by Django, and the two are essentially cognate)

So where did this WSGI come from? Let's go back a bit

This settings is an object that has obtained its properties from the settings.py configuration file in previous operations. So we just have to look for it in the settings.py profile.

Let's look for this get_wsgi_application().

It calls setup() again and, importantly, returns an instance of the WSGIHandler class.

That's WSGIAPP itself.

load_middleware() 为构建中间件堆栈,这也是wsgiapp获取setting信息的唯一途径。导入settings.py,生成中间件堆栈。

如果看过我之前那篇fastapi源码的,应该对中间件堆栈不陌生。

app入口→中间件堆栈→路由→路由节点→endpoint

所以,wsgiapp就此构建完毕,服务器传入请求至app入口,即可经过中间件到达路由进行分发。

结语:以上就是首席CTO笔记为大家整理的关于如何启动django服务器的全部内容了,感谢您花时间阅读本站内容,希望对您有所帮助,更多关于如何启动django服务器的相关内容别忘了在本站进行查找喔。

以上内容为新媒号(sinv.com.cn)为大家提供!新媒号,坚持更新大家所需的互联网后端知识。希望您喜欢!

版权申明:新媒号所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流,不声明或保证其内容的正确性,如发现本站有涉嫌抄袭侵权/违法违规的内容。请发送邮件至 k2#88.com(替换@) 举报,一经查实,本站将立刻删除。

(0)
上一篇 2023-09-23
下一篇 2023-09-23

相关推荐

发表回复

登录后才能评论