Python has become an increasingly popular programming language, and with the popularity of web applications, using Python for web development has become more and more popular. Django is a Python-based web framework created by a group of developers eager to develop high-quality web applications. Django uses MTV (Model-Template-View) as its architecture and provides some convenient web development tools and a set of web development patterns that can help developers develop high-quality web applications faster.
In this article, we will introduce Web development examples in Django, and let us explore how to use Django for Web development.
1. Install Django
Django can be installed through Python’s package manager pip. Before installing Django, make sure you have Python installed. Linux and MacOs users can install Django by entering the following command with administrator rights in the terminal: pip install Django
After the installation is complete, you can verify whether Django was successfully installed by entering the following command in the terminal: django-admin - -version
If the Django version number is output, then Django has been successfully installed.
2. Create a Django project
After installing Django, we need to create a Django project. You can create a new Django project by entering the following command in the terminal: django-admin startproject project_name
where project_name is the project name.
After creating the project, you will find that a directory named project_name has been created. In this directory, there are the following files and directories:
project_name/
├── manage.py
└── project_name
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
Among them, manage.py is the file used to manage the project, and the other files in the project_name directory are project files. configuration file.
3. Create a Django application
Creating a new Django application allows us to separate code and functions, making it easier to manage. You can create a new Django application by entering the following command in the terminal: python manage.py startapp app_name
where app_name is the name of the application.
After creating the application, you will see a directory named app_name in the project directory.
app_name/
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
└── views.py
Among them, views.py is the view file of the application. We will use the view file in the following example to complete a simple web application.
4. Writing Django views
Django’s views are similar to controllers and are used to process HTTP requests and return responses. In Django, views are functions written in Python, and the name of the function is the name of the view. Here is a simple Django view example:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello , Django!")
The above code defines a view named hello, which returns an HTTP response object and outputs the message "Hello, Django!" Next, we need to add the view to the URLconf so that the web application can route HTTP requests to the view.
5. Set up Django URLconf
URLconf is used to route HTTP requests to the corresponding Django view. In Django, URLconf is generally defined in the project's urls.py file. Here is a simple URLconf example:
urlpatterns = [
path('hello/', views.hello, name='hello'),
]
The above code maps the "/hello/" path to a view named hello. Next, we need to run the development server and then access the web application in a web browser.
6. Run Django development server
Enter the project directory in the terminal and enter the following command to run the Django development server: python manage.py runserver
After successful operation, the following output will be output Information:
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
This means that we can access http://127.0.0.1:8000/ /127.0.0.1:8000/hello/ to access the web application you just created.
7. Conclusion
Using Django for web development can effectively improve development speed and code quality. This article briefly introduces the advantages of Django, installing and configuring Django, how to create and set up Django projects and applications, how to write and add Django views and URLconfs, and finally run a Django development server. Django is a powerful web framework that provides Python developers with a more convenient web development environment, thereby accelerating the development process of web applications. I believe that by reading this article, readers have mastered the basic knowledge of web development in Django practice.
The above is the detailed content of Web development in Python: Django in action. For more information, please follow other related articles on the PHP Chinese website!