Вопрос

I have just installed Pinax on Django 1.4.5 and in the urls the home page is a TemplateView. I want to add some logic in the home page so I commented that url and created a new one that links to a view called home. for the moment I do nothing in that view, I just want to reproduce the same behaviour as in the TemplateView, ie loading the template. The template loads but I loose the Pinax variables, as SITE_NAME (I did set the site name and it does appear when directly loading the template with the TemplateView).

This is the original url:

url(r"^$", TemplateView.as_view(template_name="homepage.html"), name="home"),

This is what I use:

url(r"^$", home, name="home"),

home is loaded from a freshly created app (with manage.py startapp, the folder is at the same level as manage.py, not inside the project's folder)

and the view connected to home is:

def home(request):
    context = {}
    return render_to_response('homepage.html', context)

If I add some context, like 'SITE_NAME': 'my site', the variables are filled correctly. But I would like to have them filled automatically just like before.

Even worse, when I try to login the home page still displays the 'logged out' version although the user is properly logged in (The account settings shows up correctly for the currently logged in user).

What did I do wrong?

Это было полезно?

Решение

You have reset the context when you are returning the response - since you didn't pass in an instance of RequestContext.

The quickest way to resolve this is to use the render shortcut, that automatically passes in the correct context:

from django.shortcuts import render

def home(request):
    return render(request, 'homepage.html', {})
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top