如何在模板中记录用户名

时间:2015-11-11 21:41:43

标签: django-templates django-views

我正在使用Django 1.8和Python 3.4 我不知道为什么我的模板没有在模板profile.html上显示我的用户名:/

profile.py

{% load staticfiles %}

<link rel="stylesheet" type="text/css" href="{% static 'accounts/css/style.css' %}" />

{% block content %}
<h2>My profile</h2>
<p>{{ request.user.username }}</p>
{% endblock %}

views.py

from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.shortcuts import render_to_response
from django.http import  HttpResponseRedirect
from django.core.context_processors import csrf
from django.contrib.auth import authenticate, login


def login_view(request):

    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/accounts/profile')
            else:
                # Return a 'disabled account' error message
                ...
                pass
        else:
            # Return an 'invalid login' error message.
            pass
    form = AuthenticationForm()
    args = {}
    args.update(csrf(request))
    args['form']= AuthenticationForm()
    return render_to_response('accounts/login.html', args)

def my_view(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        print(request.user)
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect('/accounts/profile')
        else:
            # Return a 'disabled account' error message
            ...
    else:
        # Return an 'invalid login' error message.
        ...
def profile(request):
    username = request.user.username
    return render_to_response('accounts/profile.html', username)

def register_user(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/register_success')
    args = {}
    args.update(csrf(request))
    args['form']= UserCreationForm()
    return render_to_response('accounts/register_user.html', args)

def register_success(request):
    return render_to_response('accounts/register_success.html')

从django模板获取用户信息的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

django.template.context_processors.request文件中的context_processors变量的TEMPLATE个选项中添加settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',    # add this line
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]