Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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
登录django后,如何从仪表板中的数据库获取特定用户的详细信息?_Django_Django Models_Django Templates_Django Views - Fatal编程技术网

登录django后,如何从仪表板中的数据库获取特定用户的详细信息?

登录django后,如何从仪表板中的数据库获取特定用户的详细信息?,django,django-models,django-templates,django-views,Django,Django Models,Django Templates,Django Views,我有一个注册、登录和仪表板页面。注册表单包括用户名、电子邮件、密码、目标、设计、家具、房间,因此在这些表单中,用户名,电子邮件将存储在自定义用户模型中,剩余的详细信息将存储在用户需求表中,登录页面由用户名和密码组成,这些用户名和密码将经过身份验证,现在进入仪表板页面。我正在尝试显示目标、设计、家具、,特定用户在注册期间提供的房间详细信息 My views.py: from django.shortcuts import render, redirect from django.contrib i

我有一个注册、登录和仪表板页面。注册表单包括用户名、电子邮件、密码、目标、设计、家具、房间,因此在这些表单中,用户名,电子邮件将存储在自定义用户模型中,剩余的详细信息将存储在用户需求表中,登录页面由用户名和密码组成,这些用户名和密码将经过身份验证,现在进入仪表板页面。我正在尝试显示目标、设计、家具、,特定用户在注册期间提供的房间详细信息 My views.py:

from django.shortcuts import render, redirect
from django.contrib import messages, auth
from django.contrib.auth.models import User
from contacts.models import Contact
from django.shortcuts import render,HttpResponseRedirect
from django.contrib import messages,auth
from account.forms import UserForm
from account.forms import UserRequirementForm
from django.contrib.auth import authenticate, login


def register(request):
  return render(request, 'account/register.html',);
def user_register(request):
    if request.method == 'POST': # if there is a post request in the form
        user_form = UserForm(data=request.POST) #first of all it is a user_form will be posted details present in the user_form
        user_requirement_form = UserRequirementForm(data=request.POST)# after posting the details of the user_form post the details
        if user_form.is_valid() and user_requirement_form.is_valid():
         # if user_form & user_requirement form is valid

         User = user_form.save()#if form is valid save
         User.set_password(request.POST['password'])
         User.save()
         user_requirement = user_requirement_form.save(commit=False)
         # Set user
         user_requirement.user = User
         user_requirement.save()
         user_requirement_form.save_m2m()
         messages.success(request,('Project saved successfully'))
         return render(request,'account/home1.html')
        else:
          messages.warning(request, 'Please correct the errors above')
    else:  
        user_form = UserForm()
        user_requirement_form = UserRequirementForm()
    return render(request,'account/register.html', {'user_form': user_form, 'requirements_form': user_requirement_form})

def login(request):
  if request.method == 'POST':
    username = request.POST['username']
    password = request.POST['password']
    user = auth.authenticate(username=username, password=password)
    if user is not None:
      if user.is_active:
        auth.login(request, user)
        messages.success(request, 'You are now logged in')
        return redirect('dashboard')
    else:
      messages.error(request,'Invalid')
      return redirect('login')
  else:
    return render(request, 'account/login.html')

def dashboard(request):
    return render(request, 'account/dashboard.html',);
dashboard.html:

<details>
  <summary class="summary ml-4">User Details</summary>
  <p class="p1 ml-4">The contents that are selected by the user</p>
  <p class="p2 ml-4">All content and graphics on this web site are the property of the company Refsnes Data.</p>
</details>

<p class="p3 ml-4 mt-4"><b>Room:<br></b>{{user.room}}</p>
<p class="p4 ml-4"><b>GOAL:</b><br>{{user.goal}}</p>
<p class="p5 ml-4"><b>FURNITURE:</b><br>{{user.furniture}}</p>
<p class="p6 ml-4"><b>DESIGN:</b><br>{{user.design}}</p>

请求。用户
将提供当前登录用户的详细信息。这里您有与您的
用户需求
模型相关的
OneToOne
,因此您可以这样尝试

<p class="p3 ml-4 mt-4"><b>Room:<br></b>{{request.user.user_requirement.room}}</p>

房间:
{{request.user.user\u requirement.Room}

根据评论编辑

因为您的目标是一个多字段,所以需要迭代它

 <p class="p4 ml-4"><b>GOAL:</b>
     {% for goal in request.user.user_requirement.goal.all %}
    <br>{{goal}}</p>

   {% if not forloop.last %}, {% endif %} #if you want to separate goals with comma

    {% endfor %}
目标: {request.user.user_requirement.goal.all%中的目标为%}
{{goal}}

{%if不是forloop.last%},{%endif%}#如果要用逗号分隔目标 {%endfor%}
当客户端发送请求时,您可以使用
请求访问用户信息。user
@sina房间以及存储在特定用户的用户需求表中的所有其他详细信息应显示在仪表板页面确定您必须呈现模板引擎,并使用钥匙从后端传递任何用户信息;是的,我试过了,但是对于多对多关系,它不起作用。它工作得很好,但是有很多目标{{{request.user.user\u requirement.goal}当我这样付出时,我得到了回报。目标。没有人能请你帮忙吗me@sssss那么你的目标就没有了。@arjun我的目标字段与目标表有很多关系。表中有两个目标用户需求表中有两个目标。你能告诉我为什么他们不来吗。
 <p class="p4 ml-4"><b>GOAL:</b>
     {% for goal in request.user.user_requirement.goal.all %}
    <br>{{goal}}</p>

   {% if not forloop.last %}, {% endif %} #if you want to separate goals with comma

    {% endfor %}