In fact, Django comes with a built-in lightweight web server that can be used during site development. We provide this server to allow you to quickly develop your site, which means there is no need to configure a production-level web server (such as Apache) before you are ready to release the product.
But in actual development, instead of one person developing, multiple people need to be able to access this machine. So how do we configure it? Let’s take a look:
1. Create a web project
Run the
django-admin.py startproject pytab
command to create a pytab directory in the current directory.
Note:
If you have a PHP programming background, you may be accustomed to placing your code in the document root directory of the Web server (such as /var/www). In Django, you can't do this. It's not a good idea to put any Python code in the document root of your web server because you run the risk of someone seeing the code directly through the page. This is not a good thing for security. So, place the code in some directory outside of the document root.
2. Run the built-in server
and let it run so that we can access
If you are not already in the mysite directory, go into it now and run the python manage.py runserver command. You will see the following output:
Validating models...
0 errors found
Django version 1.4.3, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000 /
Quit the server with CONTROL-C.
After the above prompt appears, it will be in a waiting state. When an access comes in, some user access information will appear. For example:
[11/Jan/2013 00:47:58] "GET / HTTP/1.1" 200 1957
200 Description The request is successful, 1957 represents the size of the sent data, the unit is B.
Although this development server is great for development, be sure to avoid the idea of using this server in a production-level environment. The server could only reliably handle a single request once at a time and was not subject to any kind of security auditing.
But in actual development, instead of one person developing, multiple people need to be able to access this machine. Django thinks of this problem for us. You can use the runserver command to solve this problem:
1. Change the listening port.
If you want to change the server port, you can pass the port as a command line parameter:
python manage.py runserver 8070
2. You can also change the IP address that the server monitors. This feature is especially useful if you want to share the same development site with other developers. The following command:
python manage.py runserver 0.0.0.0:8000
will cause Django to listen on all network interfaces and IP addresses, thus allowing other computers to connect to the development server.
Now that the server is running, you can now access http://192.168.1.111:8000/ using a web browser on other computers.