Home  >  Article  >  Backend Development  >  A first look at Django: Create your first Django project using the command line

A first look at Django: Create your first Django project using the command line

王林
王林Original
2024-02-19 09:56:06982browse

A first look at Django: Create your first Django project using the command line

Django project journey: start from the command line and create your first Django project

Django is a powerful and flexible web application framework. Based on Python, it provides many tools and functions needed to develop web applications. This article will lead you to create your first Django project starting from the command line. Before starting, make sure you have Python and Django installed.

Step 1: Create the project directory
First, open the command line window and create a new directory to store your Django project. You can choose to create the project directory anywhere. Use the following command to create a directory named "myproject":

mkdir myproject

Then, enter this directory:

cd myproject

Step 2: Use Django commands Create the project
Next, you can use Django’s command line tools to create the project. Enter the following command at the command line:

django-admin startproject myproject

This will create a project directory named "myproject" and generate the necessary file and folder structure within it. The project directory will contain a file named "manage.py" and a folder with the same name, which contains the project's configuration files and other necessary files.

Now, enter the project directory:

cd myproject

Step 3: Run the project
In the project directory, you can use the following command to run your Django project:

python manage.py runserver

This will start a development server and run your project on the default localhost and port (usually http://127.0.0.1:8000/). You can open this URL in your browser and if everything is fine, you will see Django's default welcome page.

Step 4: Create an application
Now that you have successfully created a Django project, you can create an application. A Django application is a functional module that can be reused. You can use the following command to create an app named "myapp":

python manage.py startapp myapp

This will create a folder named "myapp" in the project directory, which contains the code of the app and other necessary document.

Step 5: Configure the application
After creating an application, you need to add it to the project configuration. Open the "settings.py" file in the project directory and find the "INSTALLED_APPS" section. Add the following code to it:

'myapp',

This will tell Django that you have created an app called "myapp" and that it should be included in the project.

Step 6: Create the model
Django’s model is used to define the data structure. You create models in your app's "models.py" file. Here is a simple example:

from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()

This model defines a class named "MyModel", which has a "name" field and an "age" field.

Step 7: Apply Migration
After you create or modify the model, you need to run a command to apply these changes to the database. Use the following command:

python manage.py makemigrations
python manage.py migrate

This will create a migration file and apply it to the database.

Step 8: Create a view and URL
A view is a function or method that handles HTTP requests. You create views in your app's "views.py" file. Here is a simple example:

from django.shortcuts import render
from django.http import HttpResponse

def my_view(request):
    return HttpResponse("Hello, Django!")

In the project directory, open the "urls.py" file and add the following code to it:

from django.urls import path
from myapp import views

urlpatterns = [
    path('', views.my_view, name='my_view'),
]

This will map the URL "/" to the view function named "my_view".

Step 9: Test your application
Finally, restart your development server and open http://127.0.0.1:8000/ in your browser. If everything is fine, you will see the "Hello, Django!" message returned by the view.

Congratulations! You have successfully created your first Django project and created an application within it. Now you can continue development and add more features to your app. I wish you success!

The above is the detailed content of A first look at Django: Create your first Django project using the command line. 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