开发者

Help with passing variables w/ csrfContext

I have a login page, and in my view I pass it the csrfContext variable for the csrf_token tag. However, problems arise when I try to pass more t开发者_高级运维han just that variable into the context. For example, if I use locals()

return render_to_response('base_index.html', locals())

I get a csrf error. For some reason it only works if I explicitly pass csrfContext, and only csrfContext. However, I also need to pass on other variables. How can I pass csrfContext and those variables together? Sorry if this is a convoluted question. My view code is:

def index(request):
    current = Module.objects.all()
    error = ""
    try:
        error = request.GET["alert"]
        if error == "failure":
            error = "Woops! Something went wrong. Please try again."
        elif error == "invalid":
            error = "Invalid username/password."
        else:
            error = "Unknown Error. Please try again."
    except:
        pass
    csrfContext = RequestContext(request, error, current)
    return render_to_response('base_index.html', csrfContext)

As you can see I've been experimenting with adding variables to the RequestContext, but I have no idea how to access them in the template.


I would not recommend using locals() in this way. In more complex views you may end up passing much more to the template rendering that is required.

A better way to do this is to create the RequestContext, and either pass in the values you want to add, or add them after: https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.Context


I used return render_to_response('base_index.html', locals(), csrfContext) and that worked

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜