0

Hello,

I've found quite a few answers on this site.

I was a network admin, however these days you have to wear multiple hats and as such I'm learning Python at work and my main source for answers when stuck is of course, the net.

So here is my problem. I'm converting a pre 0.96 app to 1.4 (django/python). I've fixed most of the changes however my code seems right even when compared to others projects. In fact, just the other day I was passing through the views and everything was semi-working but now I just get the following error.

Any help appreciatted, and remember I'm not exactly on target with mots of the technical jargon of django/python yet as an fyi. Cheers if you can help

**UnboundLocalError at /wikicamp/start/edit/

local variable 'page' referenced before assignment
**

===================================================================================================
Traceback:

Environment:


Request Method: GET
Request URL: http://10.3.0.249:8000/wikicamp/start/edit/

Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.admin',
 'wikicamp.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:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/corey/wikicamp/wikicamp/wiki/views.py" in edit_page
  24.     page.content=['content', 'this is the default']

Exception Type: UnboundLocalError at /wikicamp/start/edit/
Exception Value: local variable 'page' referenced before assignment

====================================================================================================



    views

    from wikicamp.wiki.models import Page
    from django.shortcuts import render
    from django.shortcuts import render_to_response
    from django.shortcuts import HttpResponseRedirect
    from django import forms
    import markdown
    import htmllib
    from django.template import RequestContext, loader
    from django.core.context_processors import csrf
    #create your views here

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

    def edit_page(request, page_name):
        c = {}
        page.content=['content', 'this is the default']
        page.name=['name', 'this is the default']
        try:
         page = Page.objects.get(pk=page_name)
         content = page.content
        except Page.DoesNotExist:
         return render_to_response("edit.html", {"page_name":page_name, "content":content}, context_instance=RequestContext(request))

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

         =============================================================================================

         models:

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

    def __unicode__(self):
        return self.name
1
Contributor
1
Reply
3
Views
4 Years
Discussion Span
Last Post by lushys
0

I fixed the problem that wasnt allowing me to get content into the edit page. However in my final view I get an error now that Page is referenced before being assigned. I edited that line out to see what would happen and I get an error that there is an indendation expected on line 45 (EOF is line 44?) I tried a %retab, and have my setting set so tab is 4 spaces so this shouldnt have happened. I even re-wrote the file and still have this issue.

Ideas?

from wikicamp.wiki.models import Page
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.shortcuts import HttpResponseRedirect
from django import forms
import markdown
import htmllib
from django.template import RequestContext, loader
from django.core.context_processors import csrf
#create your views here

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

def edit_page(request, page_name):
    c = {}
    content=['content']
    page.name=['name', 'this is the default']
    try:
     page = Page.objects.get(pk=page_name)
     content = page.content
    except Page.DoesNotExist:
     return render_to_response("edit.html", {"page_name":page_name, "content":content}, context_instance=RequestContext(request))

def save_page(request, page_name):
    c = {}
    content = request.POST['content']
    try:
     page = Page.objects.get(pk=page_name)
     content = request.GET['content']
    except Page.DoesNotExist:
     page.content = ['content']
     Page = Page(name=page_name, page_content=content)
     return HttpResponseRedirect ("wikicamp/" + "{{page_name}}" + "/")
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.