Home > Article > Backend Development > The first step to learning Django: Installation and configuration guide
Learn Django from scratch: Detailed introduction on how to install Django and configure it, specific code examples are required
Django is an open source web application framework written in Python language. It helps developers easily build powerful web applications. If you are a beginner and want to learn Django from scratch, this article will provide you with a detailed installation and configuration guide, complete with specific code examples.
Install virtual environment
When learning and developing Django projects, we recommend using a virtual environment. The virtual environment is an independent Python running environment that can help us isolate project dependencies. The command to install the virtual environment is as follows:
$ pip install virtualenv
Create a virtual environment
Create a virtual environment named myenv. Run the following command in the command line:
$ virtualenv myenv
Activate virtual environment
Go to the root directory of the virtual environment and activate the virtual environment using the following command:
$ source myenv/bin/activate
Install Django
In the activated virtual environment, use the following command to install Django:
$ pip install django
Create Django project
In the command line, enter the To create a directory for your project, run the following command to create a Django project named myproject:
$ django-admin startproject myproject
Start the development server
Go to your project root directory and run the following command to start the development server :
$ python manage.py runserver
If everything is successful, you will see output similar to the following:
Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Now, you can visit http://127.0.0.1:8000/ in your browser to view your Django project.
Create a simple Django application
In your project root directory, run the following command to create a Django application named myapp:
$ python manage.py startapp myapp
This will Create a folder called myapp in your project that contains some sample code and configuration files.
Create model
Define the model in the models.py file in the myapp folder. For example, we create a simple model to represent a user:
from django.db import models class User(models.Model): name = models.CharField(max_length=50) email = models.EmailField() def __str__(self): return self.name
Run the following command in your virtual environment to apply the model to the database:
$ python manage.py makemigrations $ python manage.py migrate
Create View
Define a simple view function in the views.py file in the myapp folder. For example, we create a view function that returns all users:
from django.shortcuts import render from .models import User def user_list(request): users = User.objects.all() return render(request, 'user_list.html', {'users': users})
Create templates
Create a folder named templates in the myapp folder and create a folder named templates in it It is the template file of user_list.html. Display the data of all users in the template file:
<!DOCTYPE html> <html> <head> <title>User List</title> </head> <body> <h1>User List</h1> <ul> {% for user in users %} <li>{{ user.name }} - {{ user.email }}</li> {% endfor %} </ul> </body> </html>
Configure URL
Configure URL routing in the urls.py file in the myproject folder. For example, we configure a URL to bind the user_list view to the '/users/' path:
from django.urls import path from myapp import views urlpatterns = [ path('users/', views.user_list, name='user_list'), ]
Through the above steps, you have successfully installed and configured Django, and created a simple Django application. This is just the basics of learning Django, there are many more in-depth topics such as forms, user authentication, and database operations. I hope this article can help you get started with Django and pave the way for your learning journey. Happy studying!
The above is the detailed content of The first step to learning Django: Installation and configuration guide. For more information, please follow other related articles on the PHP Chinese website!