search
HomeBackend DevelopmentPython TutorialEveryone should pay attention to the landmines encountered by Django

I’ve been messing with Django’s WSGI application recently. Although Django’s own runserver is very convenient, it can’t do anything for complex functions.

First I encountered a pitfall on Windows, then on Ubuntu that comes with Windows 10, and finally solved it on a virtual machine.

It can be seen from the previous article Django Notes "Django Learning Notes (2) First Web Page" that I use cmd in the windows10 system to operate Django, because the gunicorn application is in the unix system When running, an error occurs when it is forced to run on the win platform.

1. Windows encounters a pit:

First enter in cmd: pip3 install gunicorn. After the installation is successful, switch to the project directory (G:/Django/hello). The specific operation is to switch first Go to the G drive, enter G: directly, and then enter cd Django\hello. G:\Django\hello> will appear. Then run gunicorn. In the Django project, the format is: gunicorn yourproject.wsgi [-b 127.0.0.1.8000]. The fields in brackets are optional. For other command parameters, you can send gunicorn -h to get the help documentation. My project here is hello, so my command operation is: gunicorn hello.wsgi. Then, an error occurs: ModuleNotFoundError: No module named 'pwd', there is no pwd module, okay, I can't find it. The found code is posted below. We put the found pwd.py into the python3 installation directory D:\Program Files\Python\Python36\Lib. Then we ran gunicorn hello.wsgi and the result was another error: AttributeError: module 'socket' has no attribute 'AF_UNIX' , and then I looked for it online. Because it was gunicorn in the unix system, I couldn't find the sock.py file in liunx, so I switched to the unix system and gave up on windows.

G:\Django\hello>gunicorn hello.wsgi
Traceback (most recent call last):
  File "d:\program files\python\python36\lib\runpy.py", line 193, in _run_module_as_main"__main__", mod_spec)
  File "d:\program files\python\python36\lib\runpy.py", line 85, in _run_codeexec(code, run_globals)
  File "D:\Program Files\Python\Python36\Scripts\gunicorn.exe\__main__.py", line 5, in <module>
  File "d:\program files\python\python36\lib\site-packages\gunicorn\app\wsgiapp.py", line 10, in <module>from gunicorn.app.base import Application
  File "d:\program files\python\python36\lib\site-packages\gunicorn\app\base.py", line 12, in <module>from gunicorn import util
  File "d:\program files\python\python36\lib\site-packages\gunicorn\util.py", line 13, in <module>import pwd
ModuleNotFoundError: No module named 'pwd'</module></module></module></module>

ModuleNotFoundError: No module named 'pwd'

from os import * 
from pwd import * 

def get_username():return getpwuid(getuid())[0]

pwd.py

G:\Django\hello>gunicorn hello.wsgi
Traceback (most recent call last):
  File "d:\program files\python\python36\lib\runpy.py", line 193, in _run_module_as_main"__main__", mod_spec)
  File "d:\program files\python\python36\lib\runpy.py", line 85, in _run_codeexec(code, run_globals)
  File "D:\Program Files\Python\Python36\Scripts\gunicorn.exe\__main__.py", line 5, in <module>
  File "d:\program files\python\python36\lib\site-packages\gunicorn\app\wsgiapp.py", line 10, in <module>from gunicorn.app.base import Application
  File "d:\program files\python\python36\lib\site-packages\gunicorn\app\base.py", line 13, in <module>from gunicorn.arbiter import Arbiter
  File "d:\program files\python\python36\lib\site-packages\gunicorn\arbiter.py", line 18, in <module>from gunicorn import sock, systemd, util
  File "d:\program files\python\python36\lib\site-packages\gunicorn\sock.py", line 101, in <module>class UnixSocket(BaseSocket):
  File "d:\program files\python\python36\lib\site-packages\gunicorn\sock.py", line 103, in UnixSocket
    FAMILY = socket.AF_UNIX
AttributeError: module 'socket' has no attribute 'AF_UNIX'</module></module></module></module></module>

AttributeError: module 'socket' has no attribute 'AF_UNIX'

2. The pitfalls encountered by Ubuntu that comes with the win10 platform

Start the Linux system on win10: First, go to win10 settings-->Security and updates-->For developers-->Development Personnel mode-->tick, then go to Control Panel-->Program Features-->Turn Windows features on or off-->Windows Subsystem for Linux (Bata)-->tick, and finally Run shell as administrator--> Enter cmd--> Enter bash--> Follow the instructions to download and install Linux.

After the installation is completed, enter cmd with shell as administrator and enter bash to enter the Linux system. The system has python2.7 and python3.5 installed by default. The default startup of python is python2. You can set the default python to python3:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 100  sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 200

Follow the routine to install pip3 first (if you install pip directly, it will be installed in python2 by default. ): sudo apt-get install python3-pip, then use pip to install Django: sudo pip3 install Django, and finally use pip to install gunicorn: sudo pip3 install gunicorn (the automatically installed version now is 19.7.1). After the environment is installed, switch to the project address: cd /mnt/g/Django/hello, and then enter gunicorn hello.wsgi, and an error occurs again. OSError: [Errno 92] Protocol not available. Later, after uninstalling pip, I found that gunicorn depends on python-gunicorn (19.4.5), so I uninstalled gunicorn (19.7.1): pip3 uninstall gunicorn, and then installed gunicorn (19.4.5) :pip3 install gunicorn==19.4.5. Then the command gunicorn hello.wsgi started successfully, Failed to find application did not appear, and 127.0.0.1:8000/admin/ was successfully accessed.

Lee@Kein:/mnt/g/Django/hello$ gunicorn hello.wsgi
[2017-07-16 15:16:25 +0800] [428] [INFO] Starting gunicorn 19.7.1Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 44, in set_options
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
OSError: [Errno 92] Protocol not available

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/gunicorn", line 11, in <module>sys.exit(run())
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py", line 74, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py", line 203, in run
    super(Application, self).run()
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py", line 198, in run
    self.start()
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py", line 157, in start
    self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds)
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 180, in create_sockets
    sock = sock_type(addr, conf, log)
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 32, in __init__self.sock = self.set_options(sock, bound=bound)
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 89, in set_optionsreturn super(TCPSocket, self).set_options(sock, bound=bound)
  File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 46, in set_optionsif err[0] not in (errno.ENOPROTOOPT, errno.EINVAL):
TypeError: 'OSError' object is not subscriptable</module>

OSError: [Errno 92] Protocol not available

Lee@Kein:/mnt/g/Django/hello$ gunicorn hello.wsgi
[2017-07-16 15:22:16 +0800] [470] [INFO] Starting gunicorn 19.4.5[2017-07-16 15:22:16 +0800] [470] [INFO] Listening at: http://127.0.0.1:8000 (470)
[2017-07-16 15:22:16 +0800] [470] [INFO] Using worker: sync
[2017-07-16 15:22:17 +0800] [473] [INFO] Booting worker with pid: 473Not Found: /static/admin/css/base.css
Not Found: /static/admin/css/login.css

3. Install the ubuntu system on the virtual machine and follow the above routine to successfully start the service in one go. I also thought about the problems encountered on win10 and successfully filled in pit 2.

Some other problems are:

1. Django cannot install mysqlclient in Linux. If you install PyMySQL at this time, an error will be reported when running the django project. You only need to add a sentence to a file.

In the project root directory, find __init__.py in the corresponding app directory and add this sentence:

import pymysql

pymysql.install_as_MySQLdb()

Example:

My project is eagle, under eagle there is the file manage.py, and there is also your own app, under this app there is the file view.py.

2. Note the static format in nginx. Loading using location /static/ and location /static is different. It must be consistent according to the format in html.

3. It took me a day to understand a problem. Most tutorials say to open uwsgi --ini eagle.ini to link uwsgi and django, and then open nginx. However,

If you There is no log path added to eagle.ini and it will not be run in the background. At this time, uwsgi can only be closed,

and then open nginx. When you enter the address in the browser, a 502 error is found. Check /var/ log/nginx/myweb_error.log, found the error message, congratulations on getting into the trap.

The reason is that uwsgi was shut down just now and you did not restart it. Many people on the Internet do not know this reason. . .

What a rip-off, such a messy answer. According to my method, there are two methods, one is to open nginx and then run

uwsgi --ini eagle.ini, the other is to add the log path to eagle.ini and it will automatically run in the background

The above is the detailed content of Everyone should pay attention to the landmines encountered by Django. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
怎么将Django项目迁移到linux系统中怎么将Django项目迁移到linux系统中Jun 01, 2023 pm 01:07 PM

Django项目配置修改我们需要把原先的Django项目进行修改才能更好地进行项目迁移工作,首先需要修改的是settings.py文件。由于项目上线之后不能让用户看到后台的运行逻辑,所以我们要把DEBUG改成False,把ALLOWED_HOSTS写成&lsquo;*&rsquo;,这样是为了允许从不同主机进行访问。由于linux中如果不加这句可能会出现文件找不到的情况,所以我们要把模板的路径进行拼接。由于做Django项目肯定进行过数据库的同步,所以我们要把migrations

centos+nginx+uwsgi部署django项目上线的方法centos+nginx+uwsgi部署django项目上线的方法May 15, 2023 am 08:13 AM

我django项目叫yunwei,主要app是rabc和web,整个项目放/opt/下如下:[root@test-codeopt]#lsdjango_virtnginxredisredis-6.2.6yunwei[root@test-codeopt]#lsyunwei/manage.pyrbacstatictemplatesuwsgiwebyunwei[root@test-codeopt]#lsyunwei/uwsgi/cut_log.shloguwsgi.iniuwsgi.loguwsgi.p

Django框架中的数据库迁移技巧Django框架中的数据库迁移技巧Jun 17, 2023 pm 01:10 PM

Django是一个使用Python语言编写的Web开发框架,其提供了许多方便的工具和模块来帮助开发人员快速地搭建网站和应用程序。其中最重要的一个特性就是数据库迁移功能,它可以帮助我们简单地管理数据库模式的变化。在本文中,我们将会介绍一些在Django中使用数据库迁移的技巧,包括如何开始一个新的数据库迁移、如何检测数据库迁移冲突、如何查看历史数据库迁移记录等等

Django框架中的文件上传技巧Django框架中的文件上传技巧Jun 18, 2023 am 08:24 AM

近年来,Web应用程序逐渐流行,而其中许多应用程序都需要文件上传功能。在Django框架中,实现上传文件功能并不困难,但是在实际开发中,我们还需要处理上传的文件,其他操作包括更改文件名、限制文件大小等问题。本文将分享一些Django框架中的文件上传技巧。一、配置文件上传项在Django项目中,要配置文件上传需要在settings.py文件中进

如何用nginx+uwsgi部署自己的django项目如何用nginx+uwsgi部署自己的django项目May 12, 2023 pm 10:10 PM

第一步:换源输入命令换掉Ubuntu的下载源sudonano/etc/apt/sources.list将以下全部替换掉原文件,我这里用的是阿里的源,你也可以换其他的。debhttp://mirrors.aliyun.com/ubuntu/bionicmainrestricteddebhttp://mirrors.aliyun.com/ubuntu/bionic-updatesmainrestricteddebhttp://mirrors.aliyun.com/ubuntu/bionicunive

使用Django构建RESTful API使用Django构建RESTful APIJun 17, 2023 pm 09:29 PM

Django是一个Web框架,可以轻松地构建RESTfulAPI。RESTfulAPI是一种基于Web的架构,可以通过HTTP协议访问。在这篇文章中,我们将介绍如何使用Django来构建RESTfulAPI,包括如何使用DjangoREST框架来简化开发过程。安装Django首先,我们需要在本地安装Django。可以使用pip来安装Django,具体

使用Python Django框架构建博客网站使用Python Django框架构建博客网站Jun 17, 2023 pm 03:37 PM

随着互联网的普及,博客在信息传播和交流方面扮演着越来越重要的角色。在此背景下,越来越多的人开始构建自己的博客网站。本文将介绍如何使用PythonDjango框架来构建自己的博客网站。一、PythonDjango框架简介PythonDjango是一个免费的开源Web框架,可用于快速开发Web应用程序。该框架为开发人员提供了强大的工具,可帮助他们构建功能丰

Django+Bootstrap构建响应式管理后台系统Django+Bootstrap构建响应式管理后台系统Jun 17, 2023 pm 05:27 PM

随着互联网技术的快速发展和企业业务的不断扩展,越来越多的企业需要建立自己的管理后台系统,以便于更好地管理业务和数据。而现在,使用Django框架和Bootstrap前端库构建响应式管理后台系统的趋势也越来越明显。本文将介绍如何利用Django和Bootstrap构建一个响应式的管理后台系统。Django是一种基于Python语言的Web框架,它提供了丰富的功

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment