Unable to get Python function to show on homepage

I am able to get the function to display by making it into a link like so <a href="{% url 'my_view' %}">Go to My View</a>

But not able to display the function by doing {{ message }} . Any ideas why this would be?

Please post the view here that renders that page, and any other relevent and associated code.

1 Like

Thanks so much for the help. I just now got it to work. Been working on it for days. I did not know you can have multiple functions with the same name inside of Django view file. New to the forum and not sure how to mark this as solved yet. But I have solved it.

You can’t have multiple functions with the same name in the same python file.

1 Like

To be a bit more precise about it, actually you can.

However, if you want to use all of those identically-named functions, you would need to ensure that they are in separate namespaces (e.g. classes, etc). Otherwise, the latter definitions replace the earlier ones.

2 Likes

Yeah, forget to mention this.
An example of this:

1- Defining a function with equal names under the same namespace will override the previous

# some_file.py

def foo():
  print("bar")

def foo():
  # foo has the same name as the function defined above, and are on the same
  # namespace, in this case the module some_file
  # so the first foo function gets overrided by this declaration
  print("foo")

foo()
# Prints "foo"

This happens because python will store the foo function inside the module some_file on the first call, and when the second one is defined, this overrides the previous function. So what python does is to define the function foo inside the some_file namespace, like: some_file.foo.
This is the same for variables:

x = 1
x = 3
print(x)
# Prints 3

2- Defining functions with the same name, undes differente namespaces won’t override the previous:

# some_file.py

def foo():
  print("foo")


class Something:
  def foo(self):
    print("bar")

foo()
# prints "foo"

Something().foo()
# prints bar

In this case python stored foo on some_file like: some_file.foo and;
the foo function under Something class like: some_file.Something.foo.

1 Like

Hello all. Thanks for the info and the assist. I have decided to post the code for your review. I might not be doing it correctly then. Excuse any formatting issues. I am not sure how to format on here.

This is from my view.py file

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

to use my HMTL template

def index(request):
template = loader.get_template(‘index.html’)
return HttpResponse(template.render())

test print variable

def index(request):
hello = “This is the hello variable”
context = {‘hello’: hello}
return render(request, ‘index.html’, context)

This is from my url.py file

from django.urls import path
from . import views

urlpatterns = [
path(‘’, views.index, name=‘index’),
path(‘my-view/’, views.my_view, name=‘my_view’)

]

This is from my HTML document

{{ hello }}

To properly format your code, enclose each block (file, etc) of code (or template) between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code (etc), then another line of ```. Note - these lines of ``` must be lines by themselves, they must not be the beginning or end of lines with other text.

You don’t need to repost your code, you can edit your previous post to add the ``` before and after each file’s worth of code.

Please be more specific now about what is not working.

What URL are you looking at? (What URL is not producing what you’re expecting to see?)