【问题标题】:i unable to fetch user information from django admin我无法从 django admin 获取用户信息
【发布时间】:2021-08-13 12:09:46
【问题描述】:

我正在为此创建 user_edit_profile,当用户在注册时填写详细信息时,我需要显示详细信息。向用户显示所有详细信息都存储在 django admin 中,但我无法获取它,只有我可以获取用户名

<div class="form-group">
    <label for="username">Username</label>
    <input type="text" class="form-control" id="username" name="username" value={{user.get_username}}  required>
  </div>

问题图片如下:- https://drive.google.com/file/d/1q_aJYbhYmXj_j7LKKM7_XFTTKUpN7Hpe/view?usp=sharing

我的用户名印在盒子里,但其他只需要印 所以请有人帮助摆脱这种头痛。

edit_profile.html


<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">

    <title>Edit Profile</title>
    <style>
      .form-control {
    display: block;
    width: 66%;
    padding: .375rem .75rem;
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.5;
    color: #212529;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid #ced4da;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    border-radius: .25rem;
    transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
#left{
    margin-left: -218px;
    width: 36rem;
}
    </style>
  </head>
  <body>

<div class="container">
<div class="col-lg-12 my-4 mx-2">
  <h1>Update Profile</h1>
    <form action= "############" method='POST'>
      <div class="form-group">
        <label for="username">Username</label>
        <input type="text" class="form-control" id="username" name="username" value={{user.get_username}}  required>
      </div>
      <div class="form-group">
        <label for="fname">First Name</label>
        <input type="text" class="form-control" id="fname" name="fname" value={{request.user.get_firstname}}  required>
      </div>
      <div class="form-group">
        <label for="lname">Last Name</label>
        <input type="text" class="form-control" id="lname" name="lname" value={{user.get_lname}}  required>
      </div>
      <div class="form-group">
        <label for="email">Email address</label>
        <input type="email" class="form-control" id="email" name="email" value={{contact.get_Email}}  required>
      </div>
      <!-- <div class="form-group">
        <label for="pass1">Choose a password</label>
        <input type="password" class="form-control" id="pass1" name="pass1" placeholder="Choose Your Password" required>
      </div>
      <div class="form-group">
        <label for="pass2">Confirm Password</label>
        <input type="password" class="form-control" id="pass2" name="pass2" placeholder="Enter your password again" required>
      </div> -->
     
    
            {% csrf_token %}
            <button type="submit" class="btn btn-primary mt-3">Update</button>
    </form>
    <div class="change">
      <h1>Change Your Password</h1>
      <div class="col-lg-12 my-4 mx-2">
      <div class="col-md-12"><label for="pass2">Old Password</label>
        <input type="password" class="form-control" id="pass2" name="pass2" placeholder="Enter your password again" required>
      </div>
      

</div>
</div>
  
<div class="row">
  <div class="col-md-6">
    <label for="pass1">New Password</label>
    <input type="password" class="form-control" id="pass2" name="pass2" placeholder="Enter your password again" required></div>
  <div class="col-md-4" id="left">
    <label for="pass2">Confirm Password</label>
    <input type="password" class="form-control" id="pass2" name="pass2" placeholder="Enter your password again" required></div>
</div>
<button type="submit" class="btn btn-primary mt-3">Update</button>





</div>
 </div>
    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>

    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js" integrity="sha384-eMNCOe7tC1doHpGoWe/6oMVemdAVTMs2xqW4mwXrXsW0L84Iytr2wi5v2QjrP/xp" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.min.js" integrity="sha384-cn7l7gDp0eyniUwwAZgrzD06kc/tftFf19TOAs2zVinnD/C7E91j9yyk5//jjpt/" crossorigin="anonymous"></script>
    -->
  </body>
</html>




    views.py


def handlesignup(request):
    if request.method=='POST':
        username=request.POST['username']
        fname=request.POST['fname']
        lname=request.POST['lname']`enter code here`
        email=request.POST['email']
        pass1=request.POST['pass1']
        pass2=request.POST['pass2']
        myuser=User.objects.create_user(username,fname,email)
        myuser.fname=fname
        myuser.lname=lname
        myuser.email=email
        myuser.save()
        messages.success(request,'Your account has been created {{Users.username}}')
        t.sleep(2)
        return redirect('/')

【问题讨论】:

    标签: html django django-admin-filters


    【解决方案1】:

    我认为在 request.user.get_firstname 中应该没有请求?也许这就是错误?

    【讨论】:

    • 我尝试了两种方法 {{user.get_username}} 和 {{request.user.get_firstname}} 但第一种方法仅适用于用户名,当我在其他字段中运行时它不起作用
    • 你试过 {{user.get_short_name()}} 吗? docs.djangoproject.com/en/3.2/ref/contrib/auth
    • 不工作“无法解析余数:'()' from 'user.get_firstname()'”
    【解决方案2】:

    终于成功了,谢谢大家的帮助

    但解决方案是

    用户名---{{ user.get_username }}

    对于电子邮件 -------{{ user.email}}

    名字----{{ user.get_full_name }}{{user.get_short_name}}

    姓氏-----{{ user.last_name }}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 2018-10-28
      • 2018-12-20
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多