Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 匿名用户-显示用户信息_Python_Django - Fatal编程技术网

Python 匿名用户-显示用户信息

Python 匿名用户-显示用户信息,python,django,Python,Django,我试图显示我的用户信息,但我得到匿名用户作为我的输出; 我在My views.py中的代码如下: def register(request): if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): form.save() return redirect('/account') el

我试图显示我的用户信息,但我得到匿名用户作为我的输出;

我在My views.py中的代码如下:

def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/account')
    else:
        form = RegistrationForm()

        args = {'form' : form}
        return render(request, 'accounts/register.html', args)

def view_profile(request):
    args = {'user': request.user}
    return render (request, 'accounts/profile.html',args)
我超越了UserCreationForm,我在forms.py中的代码是

class RegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True)


    class Meta:
        model = User
        fields = {
            'username',
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2'
        }


    def save(self,commit=True):
        user = super(RegistrationForm,self).save(commit=False)
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.email = self.cleaned_data['email']

    if commit:
        user.save()
        return user
我希望在其中显示我的个人资料信息的My profile.html是

{% block head %}

<title> User Profile </title>

{% endblock %}

{% block body %}
<div class="container">
<p>
  <h1> {{user}}</h1>
  <h3>First Name: {{user.first_name}}</h3>
  <h3>Last Name: {{user.last_name}}</h3>
  <h3>Email: {{user.email}}</h3>
</p>
</div>


{% endblock %}
{%block head%}
用户配置文件
{%endblock%}
{%block body%}

{{user}}
名字:{{user.First_Name}
姓氏:{{user.Last_Name}
电子邮件:{{user.Email}

{%endblock%}

真的不知道我哪里出了问题。非常感谢您的帮助。

您必须用装饰您的
def view\u配置文件(请求):
,否则Django也将向匿名用户提供此请求

另外请注意,如果您有(或添加)
django.core.context\u处理器,请向您的
设置.TEMPLATE\u context\u处理器请求

(或
settings.TEMPLATES['OPTIONS']['context\u processors']
取决于您的django版本)您可以在模板中使用
{{request.user}
,而无需在上下文中创建特定条目。

您必须使用
def view\u配置文件(请求):
,否则django也将向匿名用户提供此请求

另外请注意,如果您有(或添加)
django.core.context\u处理器,请向您的
设置.TEMPLATE\u context\u处理器请求

(或
settings.TEMPLATES['OPTIONS']['context\u processors']
取决于您的django版本)您可以在模板中使用
{{request.user}
,而无需在上下文中创建特定条目。

您确定用户已登录吗?您可以查看管理员吗?您注册了一个用户,但从未登录该用户。我解决了上述问题,但现在我的问题是,即使其他用户登录,也只显示管理员用户详细信息?您确定有用户登录吗?您可以查看管理员吗?您注册了一个用户,但从未登录该用户。我修复了上述问题,但现在我的问题是,即使其他用户登录,也只显示管理员用户详细信息?