Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Django 添加测试视图


测试视图

在测试 Django 的不同方面时,最好有一个地方可以测试代码,而不会破坏主项目。

当然,这是可选的,但如果你想按照本教程的所有步骤操作,你应该添加一个测试视图,该视图与我们下面创建的视图完全相同。

然后你可以按照示例操作,并在自己的计算机上尝试。


添加视图

首先,在 views.py 文件中添加一个名为“testing”的视图

my_tennis_club/members/views.py:

from django.http import HttpResponse
from django.template import loader
from .models import Member

def members(request):
  mymembers = Member.objects.all().values()
  template = loader.get_template('all_members.html')
  context = {
    'mymembers': mymembers,
  }
  return HttpResponse(template.render(context, request))
  
def details(request, id):
  mymember = Member.objects.get(id=id)
  template = loader.get_template('details.html')
  context = {
    'mymember': mymember,
  }
  return HttpResponse(template.render(context, request))
  
def main(request):
  template = loader.get_template('main.html')
  return HttpResponse(template.render())

def testing(request):
  template = loader.get_template('template.html')
  context = {
    'fruits': ['Apple', 'Banana', 'Cherry'],   
  }
  return HttpResponse(template.render(context, request))

URL

我们必须确保进入 /testing/ 的 URL 会被重定向到测试视图。

这是在 members 文件夹中的 urls.py 文件中完成的

my_tennis_club/members/urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.main, name='main'),
    path('members/', views.members, name='members'),
    path('members/details/<int:id>', views.details, name='details'),
    path('testing/', views.testing, name='testing'),    
]

测试模板

我们还需要一个模板,我们可以在其中使用 HTML 和 Django 代码。

你可能注意到测试视图中有一个对模板的引用?

在 templates 文件夹中创建一个名为“template.html”的模板

my_tennis_club
    manage.py
    my_tennis_club/
    members/
        templates/
            404.html
            all_members.html
            details.html
            main.html
            master.html
            myfirst.html
            template.html

打开 template.html 文件并插入以下内容

my_tennis_club/members/templates/template.html:

<!DOCTYPE html>
<html>
<body>

{% for x in fruits %}
  <h1>{{ x }}</h1>
{% endfor %}

<p>In views.py you can see what the fruits variable looks like.</p>

</body>
</html>
运行示例 »

如果服务器没有运行,请导航到 /my_tennis_club 文件夹,并在命令提示符中执行此命令

py manage.py runserver

在浏览器窗口中,在地址栏中键入 127.0.0.1:8000/testing/

结果应如下所示


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.