global name 'content' is not defined

1,127 views
Skip to first unread message

Ali Shaikh

unread,
May 27, 2012, 4:58:13 AM5/27/12
to Django users
'model.py`
from django.db import models

class Page(models.Model):
name = models.CharField(max_length=20, primary_key=True)
content = models.TextField(blank=True)

# Create your models here.

`view.py`
from wiki.models import Page
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import get_object_or_404, render_to_response
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.core.context_processors import csrf


def view_page(request, page_name):
try:
page = Page.objects.get(pk=page_name)
except Page.DoesNotExist:
return render_to_response("create.html",{"page_name":page_name})
return render_to_response("view.html", {"page_name":page_name,
"content":content},context_instance=RequestContext(request))

def edit_page(request, page_name):
try:
page = Page.objects.get(pk=page_name)
content= page.contents
except Page.DoesNotExist:
content= ""
return render_to_response("edit.html", {"page_name":page_name,
"content":content},context_instance=RequestContext(request))

def save_page(request, page_name):
content= request.POST["content"]
try:
page = Page.objects.get(pk=page_name)
page.content= content
except Page.DoesNotExist:
page = Page(name=page_name,content=content)
page.save()
return HttpResponseRedirect("/wikicamp/" + page_name + "/")

Traceback


Environment:
Request Method: GET
Request URL:

Django Version: 1.4
Python Version: 2.6.6
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'wiki')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:

enter code here

File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/
base.py" in get_response
111. response = callback(request,
*callback_args, **callback_kwargs)
File "/home/tanveer/djcode/wikicamp/wiki/views.py" in view_page
16. return render_to_response("view.html", {"page_name":page_name,
"content":content},context_instance=RequestContext(request))

Exception Type: NameError at /wikicamp/start/
Exception Value: global name 'content' is not defined


PLs help..........:(

Nikhil Verma

unread,
May 27, 2012, 5:12:13 AM5/27/12
to django...@googlegroups.com
Hi

From the traceback the error is in :-


def view_page(request, page_name):
       try:
               page = Page.objects.get(pk=page_name)
       except Page.DoesNotExist:
               return render_to_response("create.
html",{"page_name":page_name})
       return render_to_response("view.html", {"page_name":page_name,
"content":content},context_instance=RequestContext(request))

Before looking further make sure everything is imported correctly.

Now, It is not able recognize content,  what is content ? 

You should do content = page.content and then assign  a key named 'content' : content just like you had.

Page is a class and content,name are its attributes.

so when you do this :-

page = Page.objects.get(pk=page_name)

# You are making a query from the Page table that will return something , page_name you are already passing it as an argument (don't know how) that's why it is not throwing an error but for content , it is an entity/attribute of page .

So make an object of that class Page and then access its attributes.

I am also not an expert in django but i think this info might help you.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Regards
Nikhil Verma
+91-958-273-3156

Nevio Vesic

unread,
May 27, 2012, 5:18:57 AM5/27/12
to django...@googlegroups.com
You're missing a `content` at def view_page. I've marked possible solution (as red) that could fix it. 

def view_page(request, page_name):
       try:
               page = Page.objects.get(pk=page_name)
               content = page.content

       except Page.DoesNotExist:
               return render_to_response("create.html",{"page_name":page_name})
       return render_to_response("view.html", {"page_name":page_name, "content":content},context_instance=RequestContext(request))

Tanveer Ali Sha

unread,
May 27, 2012, 7:18:07 AM5/27/12
to django...@googlegroups.com
thank you nikhil and nevio.......:)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/nojisJrWsyQJ.
Reply all
Reply to author
Forward
0 new messages