Django 简明教程

Django - Index Page

当使用 startproject command 创建新 Django 项目时,URL http://localhost:8000/ 将显示 default index page 。它表明 Django 安装已成功完成。

When a new Django project is created with the startproject command, the URL http://localhost:8000/ shows a default index page. It shows that the Django installation is done successfully.

使用以下命令创建一个项目-

Create a project with the following command −

django-admin startproject myproject

现在,您的项目已创建和配置好,确保它正在工作-

Now that your project is created and configured, make sure it’s working −

python manage.py runserver

在运行上述命令时,您将在您的屏幕上看到类似以下内容-

On running the above command, you will get to see something like the following on your screen −

Validating models...

0 errors found
March 09, 2022 - 12:24:26
Django version 4.0, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.Quit the server with CONTROL-C.

开发服务器在运行 http://127.0.0.1:8000/ 。在浏览器中打开链接。

The development server is running at http://127.0.0.1:8000/. Open the link in the browser.

django index page 1

在主“myproject”文件夹中,使用 manage.py 命令-

In the main "myproject" folder, use the manage.py command −

python manage.py startapp myapp

您刚刚创建了 myapp 应用程序。Django还会创建一个包含应用程序结构的“myapp”文件夹-

You just created the myapp application. Django also creates a "myapp" folder with the application structure −

myapp/
   __init__.py
   admin.py
   models.py
   tests.py
   views.py
  1. init.py − Just to make sure python handles this folder as a package.

  2. admin.py − This file helps you make the app modifiable in the admin interface.

  3. models.py − This is where all the application models are stored.

  4. tests.py − This is where your unit tests are.

  5. views.py − This is where your application views are.

更新项目 settings.py 文件中INSTALLED_APPS列表(添加您的应用程序名称)-

Update the INSTALLED_APPS list in the settings.py file of your project (add your app name) −

INSTALLED_APPS = [
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'myapp',]

我们将在 myapp 中创建一个简单的视图来说“ welcome to my app!

We will create a simple view in myapp to say "welcome to my app!"

打开“myapp\views.py”并添加以下 view 函数-

Open "myapp\views.py" and add the following view function −

from django.shortcuts import render

# Create your views here.

def index(request):
   # other view code here
   return render(request, index.html', {})

在此视图中,我们使用 HttpResponse 来呈现HTML页面。为了将此视图作为页面查看,我们只需要将其映射到一个URL上。

In this view, we use HttpResponse to render the HTML page. To see this view as a page, we just need to map it to a URL.

将以下Python脚本另存为 myapp/urls.py -

Save the following Python script as myapp/urls.py

from django.urls import path
from . import views

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

下一步是将根 URLconf 指向 myapp.urls 模块。

The next step is to point the root URLconf at the myapp.urls module.

myproject/urls.py 中,添加一个用于 django.urls.include 的导入,并在 urlpatterns 列表中插入一个include(),这样您就有-

In myproject/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list, so you have −

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
   path('', include('myapp.urls')),
   path('admin/', admin.site.urls),
]

现在运行Django开发服务器-

Now run the Django development server −

python manage.py runserver

访问以下URL以 verify ,该 hello() view 被呈现-

Visit the following URL to verify that the hello() view is rendered −

http://localhost:8000/

您应该可以看见索引页面的输出。

You should be able to see the output of the index page.

A Django Home Page with Multiple Apps

但是,一个Django项目可能包含多个应用程序。因此,项目主页应包含指向各个应用程序主页的链接。

However, a Django project may have more than one apps in it. Hence, the project home page should contain the links to the home pages of individual apps.

让我们在当前Django项目中创建两个应用程序-

Let us create two apps in the current Django project −

python manage.py startapp customers

并且,

And,

python manage.py startapp products

Rewrite the main index page 项目如下:

Rewrite the main index page of the project as −

<!Doctype html>
<html>
<body>
   <!--header-->
   <div style="height:10%;">
      <h2 align="center">My Web Application</h2>
      <hr>
   </div>
   <div style="width:100%;">
      <!—side bar-->
      <div style="width:20%; float:left; border-right-style:groove">
         <ul>
            <b>
               <li><a href="admin/">Admin</a></li>
               <li><a href="customers/">Customers</a></li>
               <li><a href="Products/">Products</a></li>
            </b>
         </ul>
      </div>
      <!--contents-->
      <div style="margin-left:21%;">
         <p>
            <h2 align="center">Main Index Page</h2>
         </p>
      </div>
   </div>
   <br><br><br>
   <!--footer-->
   <hr>
   <div>
      <h4 align="right">All rights reserved</h4>
   </div>
</body>
</html>

客户应用程序的索引页面应保存在 templates 目录中。它是由customers/views.py 文件中的视图呈现的:

The index page for the customer app should be saved in the templates directory. It is rendered by the view in customers/views.py file −

from django.shortcuts import render

# Create your views here.
def index(request):
   return render(request, "customerindex.html", {})

与此类似, index page 是为产品应用程序创建的,且其映射视图在 “ products/views.py ” 文件中定义:

Similarly, the index page for products app is created and its mapped view is defined in the "products/views.py" file −

from django.shortcuts import render

# Create your views here.
def index(request):
   return render(request, "productindex.html", {})

您还需要按以下方式为每个应用程序定义 urlpattern 列表:

You also need to define the urlpattern list for each app as −

from django.urls import path
from . import views

urlpatterns = [
   path("", views.index, name="customer-index"),
]

同样,按以下方式处理 products 应用程序:

Similarly, do the following for the products app −

from django.urls import path
from . import views

urlpatterns = [
   path("", views.index, name="product-index"),
]

更新 myproject/urls.py 文件的 URLCONF:

Update the URLCONF of the myproject/urls.py file −

from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [
   path('admin/', admin.site.urls),
   path('products/', include("products.urls")),
   path('customers/', include("customers.urls")),
   path('', views.index, name='index'),
]

运行 Django 服务器,并访问 Django 项目的根 URL ( http://localhost:8000/ ):

Run the Django server and visit the root URL of the Django project (http://localhost:8000/) −

django index page 2