Home  >  Article  >  Backend Development  >  The first step to learning Django: Installation and configuration guide

The first step to learning Django: Installation and configuration guide

WBOY
WBOYOriginal
2024-02-18 21:47:07461browse

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.

  1. Installing Python
    Before you begin, make sure you have Python installed on your computer. You can download the Python version suitable for your system from the official Python website (https://www.python.org/downloads/) and install it according to the installation wizard.
  2. 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
  3. Create a virtual environment
    Create a virtual environment named myenv. Run the following command in the command line:

    $ virtualenv myenv
  4. 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
  5. Install Django
    In the activated virtual environment, use the following command to install Django:

    $ pip install django
  6. 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
  7. 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.

  8. 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.

  9. Configure Django application
    Open the settings.py file in the root directory of your project, find the INSTALLED_APPS option, and add 'myapp' to it.
  10. 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
  11. 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})
  12. 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>
  13. 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'),
    ]
  14. Run the project
    Restart the development server, and then access http://127.0.0.1: 8000/users/ to view the list of all users.

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!

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