Django 无法在配置文件页面中显示我们的详细信息?

Django 无法在配置文件页面中显示我们的详细信息?,django,Django,我已经用Django创建了一个项目。登录和注册工作正常,但我无法在配置文件页面上显示模型用户数据,我尝试了很多方法,但信息无法显示。仅显示与管理员用户相关的数据 views.py def maas(request,maas_username_slug): context_dict = {} try: maas = Maas.objects.get(slug=maas_username_slug) context_dict['maas_username']

我已经用Django创建了一个项目。登录和注册工作正常,但我无法在配置文件页面上显示模型
用户
数据,我尝试了很多方法,但信息无法显示。仅显示与管理员用户相关的数据

views.py

 def maas(request,maas_username_slug):
   context_dict = {}

   try:
      maas = Maas.objects.get(slug=maas_username_slug)
      context_dict['maas_username'] = maas.username
      context_dict['maas_username_slug'] = maas_username_slug
      context_dict['maas_phone'] = maas.phone
      context_dict['maas_firstname'] = maas.firstname
      context_dict['maas_lastname'] = maas.lastname
      context_dict['maas_location'] = maas.location
      context_dict['date_of_birth'] = maas.date_of_birth
      context_dict['comments'] = maas.comments
      context_dict['maas_gender'] = maas.gender
      context_dict['photo'] = maas.photo
      context_dict['maas'] = maas
   except Maas.DoesNotExist:
      pass
   print(context_dict)
   return render(request, 'testapp/profile.html', {'context_dict': context_dict})
url.py

url(r'(?P<maas_username_slug>\w+)/$', views.maas, name='profile'),
profile.html

{%extends 'testapp/base.html'%}
{%block body_block%}
<h1>Profile page</h1>
<h1>{{ maas_username }} welcome to your profile page</h1>

<li>{{maas_username}}</li>
<li>{{maas_phone}}</li>
<li>{{maas_lastname}}</li>

{%endfor%}
{%extends'testapp/base.html%}
{%block body_block%}
个人资料页
{{maas_username}欢迎来到您的个人资料页面
  • {{maas_username}}
  • {{maas_phone}}
  • {{maas_lastname}
  • {%endfor%}
    重命名
    context\u dict
    context
    ,作为回报
    render(请求'testapp/profile.html',context)

    还有一件事,如果您的
    try block fail
    ,则
    上下文将为None
    ,因此它是
    不打印任何内容

    更新

    def maas(request,maas_username_slug):
       context_dict = {}
    
       try:
          maas = Maas.objects.get(slug=maas_username_slug)
          context_dict['maas_username'] = maas.username
          context_dict['maas_username_slug'] = maas_username_slug
          context_dict['maas_phone'] = maas.phone
          context_dict['maas_firstname'] = maas.firstname
          context_dict['maas_lastname'] = maas.lastname
          context_dict['maas_location'] = maas.location
          context_dict['date_of_birth'] = maas.date_of_birth
          context_dict['comments'] = maas.comments
          context_dict['maas_gender'] = maas.gender
          context_dict['photo'] = maas.photo
          context_dict['maas'] = maas
       except Maas.DoesNotExist as ex:
          # if your code get exception than context_dict will be none.
          # that's why in html their is nothing to print.
          # I said it above 
          context_dict['error'] = ex
       print(context_dict)
       return render(request, 'testapp/profile.html', context=context_dict)
    
    html中

    <h1>Profile page</h1>
    <h1>{{ error }} </h1> <!-- here if error occured than error message will pring -->
    <h1>{{ maas_username }} welcome to your profile page</h1>
    
    <li>{{maas_username}}</li>
    <li>{{maas_phone}}</li>
    <li>{{maas_lastname}}</li>
    
    Profile页面
    {{error}}
    {{maas_username}欢迎来到您的个人资料页面
    
  • {{maas_username}}
  • {{maas_phone}}
  • {{maas_lastname}

  • 这是因为模板上的上下文与您的期望不一样。在这种情况下,您可以在模板上访问以下值:

    {%extends 'testapp/base.html'%}
    {%block body_block%}
    <h1>Profile page</h1>
    <h1>{{ context_dict.maas_username }} welcome to your profile page</h1>
    
    <li>{{ context_dict.maas_username}}</li>
    <li>{{ context_dict.maas_phone}}</li>
    <li>{{ context_dict.maas_lastname}}</li>
    
    {%endfor%}
    
    或:

    希望有帮助

    而不是

    <h1>{{ maas_username }} welcome to your profile page</h1>
    
    <li>{{maas_username}}</li>
    <li>{{maas_phone}}</li>
    <li>{{maas_lastname}}</li>
    
    {{maas_username}欢迎来到您的个人资料页面
    
  • {{maas_username}}
  • {{maas_phone}}
  • {{maas_lastname}
  • 您需要将这些更改为:

    <h1>{{ context_dict.maas_username }} welcome to your profile page</h1>
    
    <li>{{context_dict.maas_username}}</li>
    <li>{{context_dict.maas_phone}}</li>
    <li>{{context_dict.maas_lastname}}</li>
    
    {{context\u dict.maas\u username}欢迎来到您的个人资料页面
    
  • {{context_dict.maas_username}
  • {{context_dict.maas_phone}
  • {{context_dict.maas_lastname}

  • 原因是您正在传递字典(单个对象),因此如果您将dict名称命名为“context”,则需要尝试检索它的键值 那可能是

    {% for context in context %}
    <h1>{{ context.maas_username }} welcome to your profile page</h1>
    <li>{{ context.maas_username}}</li>
    <li>{{ context.maas_phone}}</li>
    <li>{{ context.maas_lastname}}</li>
    {% endfor %}
    
    {上下文%中上下文的%
    {{context.maas_username}欢迎来到您的个人资料页面
    
  • {{context.maas_username}
  • {{context.maas_phone}
  • {{context.maas_lastname}
  • {%endfor%}
    模板代码在哪里?请检查我保存的模板代码。当我使用Maas.objects.all()时。我可以在配置文件上显示数据,但所有用户的数据都将从数据库中显示。我只需要当前登录用户的详细信息。您的打印语句是否显示您正在拾取记录?'testapp/profile.html'已替换为'template\u url'对吗?我已更改但未显示您是否已检查它是否与正在呈现的模板相同。只需在执行HTTPResponse在视图中,并向我显示输出的数据,不在模板中显示控制台上的任何数据,也不打印任何内容。尝试打印
    maas
    mass\u username\u slug
    i uesd return render(请求,'testapp/profile.html',context=context\u dict),但不打印annthing
    print(context\u dict)
    您是否看到控制台上打印的此变量?我只是担心基本模板中可能不存在该块,所以我认为您可以先将其移出块的一侧进行测试,然后@manuHow about
    maas\u username\u slug
    ?它包含任何值吗?如果是,则问题是数据库中没有与您的查询匹配的Mass对象。没有maas\u username\u slug不包含任何仅在views OnlyOW中提到的值。。你能告诉我你用来访问那个视图的url吗?我想知道如果没有
    maas\u username\u slug
    ,您如何访问该视图。
    <h1>{{ maas_username }} welcome to your profile page</h1>
    
    <li>{{maas_username}}</li>
    <li>{{maas_phone}}</li>
    <li>{{maas_lastname}}</li>
    
    <h1>{{ context_dict.maas_username }} welcome to your profile page</h1>
    
    <li>{{context_dict.maas_username}}</li>
    <li>{{context_dict.maas_phone}}</li>
    <li>{{context_dict.maas_lastname}}</li>
    
    {% for context in context %}
    <h1>{{ context.maas_username }} welcome to your profile page</h1>
    <li>{{ context.maas_username}}</li>
    <li>{{ context.maas_phone}}</li>
    <li>{{ context.maas_lastname}}</li>
    {% endfor %}