简体   繁体   中英

global name 'localset' is not defined

I get this error.

global name 'localset' is not defined

Following is my code.

from django.shortcuts import render, render_to_response, RequestContext
from forms import SignUpForm


# Create your views here.
def home(request):
    form= SignUpForm(request.POST or None)
    if form.is_valid():
    save_it=form.save(commit=False)
    save_it.save()
    return render_to_response("signup.html", localset, context_instance= RequestContext(request))

In the last line of code:

return render_to_response("signup.html", localset, context_instance= RequestContext(request))

The variable localset is not defined.

@Amin A answer is correct for your original question. You do have a variable set within the render_to_response function which is not defined within your view function.

You should change the render_to_response line to look like this:

return render_to_response(
    'signup.html', context_instance=RequestContext(request)
)

There is something which you should be aware of here also. According to the django documentation the use of context_instance has been deprecated since django 1.8.

Deprecated since version 1.8: The context_instance argument is deprecated. Use the render() function instead which always makes RequestContext available.

You should now be using the render() function if you are passing back a context. This will now mean you code will look something like this:

from django.shortcuts import render
from forms import SignUpForm


# Create your views here.
def home(request):
    form= SignUpForm(request.POST or None)
    if form.is_valid():
        save_it=form.save(commit=False)
        save_it.save()
    return render(request, "signup.html")

from django.shortcuts import render, render_to_response, RequestContext from forms import SignUpForm

Create your views here.

def home(request): form= SignUpForm(request.POST or None) if form.is_valid(): save_it=form.save(commit=False)

    save_it.save()
return render_to_response("signup.html", locals(), context_instance= RequestContext(request))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM