我是Django的新手。我想在模板中显示 用户在连接时的信息,但是当我更改页面时,数据会消失!
的 views.py: 的
def login(request):
if request.method == "POST":
username = request.POST.get('username', None)
password = request.POST.get('password', None)
user = authenticate(username=username, password=password)
data = {}
data['user'] = user
if user is not None:
if user.is_authenticated:
return redirect(accueil)
else:
messages.error(request, 'Compte inactif.')
else:
messages.error(request, 'Identifiant et/ou mot de passe invalide.')
return render(request, 'login.html')
的 Models.py: 的
class AbstractCustomerUser(AbstractBaseUser):
"""
An abstract base class implementing a fully featured User model with
admin-compliant permissions.
Username, password and email are required. Other fields are optional.
"""
username = models.CharField(_('username'), max_length=30, unique=True,
help_text=_('Required. 30 characters or fewer. Letters, digits and '
'@/./+/-/_ only.'),
validators=[
validators.RegexValidator(r'^[\w.@+-]+$', _('Enter a valid username.'), 'invalid')
])
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
email = models.EmailField(_('email address'), blank=True, unique=True)
is_staff = models.BooleanField(_('staff status'), default=False,
help_text=_('Designates whether the user can log into this admin '
'site.'))
is_active = models.BooleanField(_('active'), default=True,
help_text=_('Designates whether this user should be treated as '
'active. Unselect this instead of deleting accounts.'))
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
company = models.ForeignKey(Customer)
referent_for_customers = models.ManyToManyField('Customer',
related_name='referents',
verbose_name="est référent pour")
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['id', 'company']
class Meta:
# For an unknown reason, fails to save a modified object with this
# constaint. Comment it for now:
#unique_together = ('username', 'company')
verbose_name = _('utilisateur')
abstract = True
class CustomerUser(AbstractCustomerUser):
class Meta(AbstractCustomerUser.Meta):
db_table = 'customers_customeruser'
swappable = 'AUTH_USER_MODEL'
verbose_name = 'utilisateur portail'
的模板: 的
<div id="header">
<h2>Portail Client</h2>
Bienvenue {{ user.username }}
CustomerUser table info: {{ user.company_id }}, {{ user.last_login }}
Customer table info: {{ user.company.name }}
<a href="/referents/password_change/">Modifier mot de passe</a>
<a href="/referents/logout/">Déconnexion</a>
</div>
答案 0 :(得分:1)
user
通过上下文处理器自动提供给模板。但是这些仅在您渲染模板时使用RequestContext
时才会运行:要么通过专门传递(例如使用context_instance
参数到render_to_response
),要么使用较新的{{ 1}}捷径。
所以在你所有的其他观点中,你需要确定你在做什么:
render
答案 1 :(得分:0)
看起来模板中没有用户对象。如果模板中没有用户对象,则模板评估{{ user.username }}
来自docs -
如果使用不存在的变量,模板系统将使用 插入TEMPLATE_STRING_IF_INVALID设置的值,即 默认设置为''(空字符串)。
您可以通过修改返回欢迎模板的相应视图将用户对象传递给模板,使用render()
强制隐式使用RequestContext
或使用render_to_response()
必须明确传递RequestContext
实例
答案 2 :(得分:0)
你应该使用RequestContext
from django.template import RequestContext
from django.shortcuts import render_to_response
def login(request):
args={}
...
return render_to_response('login.html', args, context_instance=RequestContext(request))