<p>嘿,伙计们,谢谢你的帮助,是的,我知道使用全局变量是不正确的方法,但我没能让它工作,但现在它起作用了,下面是代码:</p>
<pre><code>def showAddRecipe(request):
#global objc
if "userid" in request.session:
objc["ErrorMsgURL"]= ""
try:
urlList= request.POST
URL= str(urlList['url'])
URL= URL.strip('http://')
URL= "http://" + URL
recipe= __addRecipeUrl__(URL)
if (recipe == 'FailToOpenURL') or (recipe == 'Invalid-website-URL'):
#request.session["ErrorMsgURL"]= "Kindly check URL, Please enter a valid URL"
objc["ErrorMsgURL"]= "Kindly check URL, Please enter a valid URL"
print "here global_context =", objc
arurl= HttpResponseRedirect("/project3/add/import/")
arurl['ErrorMsgURL']= objc["ErrorMsgURL"]
#return HttpResponseRedirect("/project3/add/import/")
#return render_to_response('addRecipeUrl.html', objc, context_instance = RequestContext(request))
return (arurl)
else:
objc["recipe"] = recipe
return render_to_response('addRecipe.html',
objc,
context_instance = RequestContext(request))
except:
objc["recipe"] = ""
return render_to_response('addRecipe.html',
objc,
context_instance = RequestContext(request))
else:
request.session['red']= "/project3/add"
return HttpResponseRedirect("/project3/login")
def showAddRecipeUrl(request):
if "userid" in request.session:
return render_to_response('addRecipeUrl.html',
objc,
context_instance = RequestContext(request))
else:
request.session['red']= "/project3/add/import"
return HttpResponseRedirect("/project3/login")
def showLogin(request):
obj = {}
obj["error_message"] = ""
obj["registered"] = ""
if request.method == "POST":
if ('red' not in request.session) or (not request.session["red"]):
print "in if "
next= '/project3/recipes/'
else:
print "in else"
next= request.session["red"]
try:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
except:
user = authenticate(request=request)
if user is not None:
if user.is_active:
login(request, user)
request.session["userid"] = user.id
# Redirect to a success page.
return HttpResponseRedirect(next)
</code></pre>