019-Django Test View

Objectives: Django Test View

Django Test View

Django Test View

1. Purpose of a Test View

A test view is a temporary view you can use to experiment with Django code without affecting your main project. You can test templates, context data, loops, and tags safely.

2. Add the Test View

Open views.py in your members app and add the following:

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

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

3. Add URL for the Test View

Edit urls.py in the members folder:

from django.urls import path
from . import views

urlpatterns = [
  path('', views.main, name='main'),
  path('members/', views.members, name='members'),
  path('members/details/', views.details, name='details'),
  path('testing/', views.testing, name='testing'),
]

4. Create a Test Template

Create template.html in the templates folder:




{% for x in fruits %}
  

{{ x }}


{% endfor %}

In views.py you can see what the fruits variable looks like.


5. Run the Server and Test

If the server is not running, start it:

python manage.py runserver

Then open in the browser:

127.0.0.1:8000/testing/

Tip:

  • You can replace the fruits variable with any list or queryset to test loops and templates.
  • This view is purely for experimentation and does not affect your main pages.
  • You can add more Django tags or HTML to this template to practice.

6. Expected Result

  • Apple
  • Banana
  • Cherry

All displayed as <h1> headings from the template loop.

Reference Book: N/A

Author name: SIR H.A.Mwala Work email: biasharaboraofficials@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::