Passing variables page 25


#1

Hi Everyone,

Sorry every for my bad poetry yesterday, too much sparkling red and new tv series “punisher” on netflix.

So anyway, came to passing the variable from views.py

from django.shortcuts import render

# Create your views here.
def index(request):
    # this is your new view
    return render(request, 'index.html')

# contact number
def index(request):
#   variable contact number
    cn = 1
    return render(request, 'index.html', {'number': number})

And I’m getting the error "global name ‘number’ is not defined. Maybe I miss something.
index.html

{% extends 'base.html' %}
{% block title %}
    Homepage - {{ block.super }}
{% endblock title %}

{% block content %}
    <h1>Von Rat</h1>
    
    <p>{{ number }}</p>
{% endblock content %}

Cheers tc :P

Ps here’s my thinking, with python I normal import stuff at the top of the program however, I think this is done in urls.py. Anyway and tips would be appreciated.


#2

All good, I’ll look up render() and go from there. Have a good one.


#3

You just need to change it so you only have one def index (you have two) and update the output number to your number, which is defined as cn. So:

from django.shortcuts import render
# Create your views here.
# contact number
def index(request):
#   variable contact number
    cn = 1
    return render(request, 'index.html', {'cn': cn})

(You could also have changed cn to number and kept render the same, or, in render, did 'number': cn). Lots of different ways to define your variables and then pass them into the template. :)


#4

Well you beat me too it, I had too much wheat I think, feeling blah, I don’t know how people focus for 8 hours at home.

Yeah, originally I had only one def, sigh, well hello anyway, pleased to meet you and thank you for your book. Learning it to write a web page for my bro who is going from sales to mowing acreages and he wants a simple web page. That it’ll be.

Rhetorical question, why is coding documentation i.e. render() django doc so bloody hard to understand.

cheers tc

ps sorry, not sure what I’ve done now, lol.

[code]Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.9.6
Python Version: 2.7.12
Installed Applications:
[‘collection’,
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’]
Installed Middleware:
[‘django.middleware.security.SecurityMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.auth.middleware.SessionAuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’]

Template error:
In template /home/tc/projects/webapp/collection/templates/index.html, error at line 0
Reverse for ‘about’ with arguments ‘()’ and keyword arguments ‘{}’ not found. 0 pattern(s) tried: [] 1 : {% extends ‘base.html’ %}
2 : {% block title %}
3 : Homepage - {{ block.super }}
4 : {% endblock title %}
5 :
6 : {% block content %}
7 :

Von Rat


8 :
9 :

{{ cn }}


10 : {% endblock content %}

Traceback:

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py” in get_response
149. response = self.process_exception_by_middleware(e, request)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py” in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File “/home/tc/projects/webapp/collection/views.py” in index
6. return render(request, ‘index.html’, {‘cn’: cn},)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/shortcuts.py” in render
67. template_name, context, request=request, using=using)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/loader.py” in render_to_string
97. return template.render(context, request)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/backends/django.py” in render
95. return self.template.render(context)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/base.py” in render
206. return self._render(context)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/base.py” in _render
197. return self.nodelist.render(context)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/base.py” in render
992. bit = node.render_annotated(context)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/base.py” in render_annotated
959. return self.render(context)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py” in render
173. return compiled_parent._render(context)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/base.py” in _render
197. return self.nodelist.render(context)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/base.py” in render
992. bit = node.render_annotated(context)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/base.py” in render_annotated
959. return self.render(context)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py” in render
513. six.reraise(*exc_info)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py” in render
499. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py” in reverse
600. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File “/home/tc/projects/webapp/venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py” in _reverse_with_prefix
508. (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /
Exception Value: Reverse for ‘about’ with arguments ‘()’ and keyword arguments ‘{}’ not found. 0 pattern(s) tried: []

[/code]


#5

Maybe I should go back to the working version on github. If i pull it I should be ok, soz half speaking to myself. New to github push/pull.


#6

Ok, no luck, not sure where I went wrong. Will delete and start again. :/ Sorry for being a pain


#7

Ok all sorted. Started from scratch.


#8

No worries! :D