Django not evaluating base template in views
I'm quite new to Django and I just set up my first registration page with django-registration and everything worked well (users can register, change passwords etc.). Now I want to expand my app a bit so I wanted to add a simple profile page so when a user logs in he/she can see their profile. So I created a profile_page.html template to extend the base template and in my views set up a very simple view:
@login_required
def profile_info_view(request, template_name='profile/profile_page.html'):
user_profile = request.user.username
return render_to_response(template_name,{ "user":user_profile })
My base template looks like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css" />
<link rel="stylesheet" href="{{ STATIC_URL }}css/reset.css" />
{% block extra_head_base %}
{% endblock %}
<title>{% block title %}User test{% endblock %}</title>
</head>
<body>
<div id="header">
{% block header %}
{% if user.is_authenticated %}
{{ user.username }} |
<a href="{% url auth_password_change %}">{% trans "Profile" %}</a> |
<a href="{% url index %}">{% trans "Home" %}</a> |
<a href="{% url auth_logout %}">{% trans "Log out" %}</a>
{% else %}
<a href="{% url auth_login %}">{% trans "Log in" %}</a> | <a href="{% url registration_register %}">{% trans "Sign up" %}&开发者_JAVA技巧lt;/a>
{% endif %}
{% endblock %}
</div>
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
</html>
and the profile_pages.html template is:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
Hi, {{ user }}
{% endblock %}
and url.py:
urlpatterns = patterns('',
(r'^accounts/', include('registration.urls')),
(r'^profile/', profile_info_view),
(r'^$', direct_to_template,{ 'template': 'index.html' }, 'index'),
)
urlpatterns += staticfiles_urlpatterns()
So I want it to be that when a logged in user goes to the profile page (example.com/profile/) it shows the profile page and the login page if a user has not logged in.
But when a logged in user goes to /profile it evaluates the base template as if the user hasn't registered (is shows Log in in the header) but it does show the profile result. Moreover the static files do not work as well?
Any clues to why this might be happening?
p.s. I've included the template dirs in settings.py
Thanks for any help!
As dm03514 says in the comments, you're passing the username - a string - to the template as the user
variable, instead of the actual user object. The username string doesn't have the method is_authenticated
, so your check returns False.
In fact, you shouldn't pass the user to the template context at all. Instead, use RequestContext, which uses context processors to add various items to the context - including the user.
return render_to_response(template_name, {}, context_instance=RequestContext(request))
精彩评论