skip to Main Content

I’m new to Django. My views.py file looks like this –

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

# Create your views here.
def home(request):
    profiles = twitterData.getTwitterProfileData()
    return render(request, "blog/home.html", profiles)

This code works fine, profiles is simply a dict of user profile names gathered from Twitter api. Only thing is I want to put this also {“Title”: “Home”}. This would allow the page to display the correct title.

I try and write the code like this –

# Create your views here.
def home(request):
    profiles = twitterData.getTwitterProfileData()
    return render(request, "blog/home.html", {profiles, "title": "Home"})

and it doesn’t run. How can I use multiple items to send to page?

2

Answers


  1. You can create a dictionary that contains all data you want to render in your view.

     def home(request):
         profiles = twitterData.getTwitterProfileData()
         context = {
            'profiles': profiles,
            'title': "home",
         }
         return render(request, "blog/home.html", context=context)
    

    access it in your template like:
    {{ profiles }} and {{ title }}

    Login or Signup to reply.
  2. It should be like this:

    return render(request, "blog/home.html", {'profiles': profiles, 'title': "Home"})
    

    or you can use context as suggested before.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search