简体   繁体   English

对于这种特殊情况,如何在 Django 页面中的 HTML 页面中显示 model 字段值?

[英]How do I show model field values in HTML page, in Django for this particular case?

I need to display the details of the user on the profilepage.我需要在个人资料页面上显示用户的详细信息。 But in the following situation, I am unable to render phone number and flag(attributes of SpecialUser model) on my profile page.但是在以下情况下,我无法在我的个人资料页面上呈现电话号码和标志(SpecialUser 模型的属性)。 I was asked to implement an extended model for the User model in Django auth for an application.我被要求在 Django 身份验证中为应用程序的用户 model 实施扩展的 model。 I introduced 2 new fields ie, phonenumber(charfield), flag(booleanfield).我介绍了 2 个新字段,即 phonenumber(charfield)、flag(booleanfield)。 My form is able to take both the inputs.我的表单能够同时接受这两个输入。 But I couldn't render these values again into my HTML file.但是我无法将这些值再次渲染到我的 HTML 文件中。 Could someone help me out!有人可以帮帮我吗!

models.py模型.py

# accounts.models.py
from django.db import models
from django.contrib.auth.models import User


class SpecialUser(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    flag = models.BooleanField(verbose_name="Special User", default=False)
    phonenumber = models.CharField(max_length=10, verbose_name="phonenumber")

    def __str__(self):
        return self.user.username

forms.py forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import SpecialUser


class RegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ["username", "email", "password1", "password2"]


class SuperUserForm(forms.ModelForm):
    class Meta:
        model = SpecialUser
        fields = ["flag", "phonenumber"]

views.py视图.py

from accounts.models import SpecialUser
from django.shortcuts import render, redirect
from .forms import RegisterForm, SuperUserForm
from django.contrib import messages
from django.contrib.auth.models import auth


def register(request):
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        sp_form = SuperUserForm(request.POST)
        if form.is_valid() and sp_form.is_valid():
            user = form.save()
            sp_form = sp_form.save(commit=False)
            sp_form.user = user
            sp_form.save()
            messages.success(request, 'Account created!')
            return redirect('login')

    else:
        form = RegisterForm(request.POST)
        sp_form = SuperUserForm(request.POST)
        messages.warning(request, 'Your account cannot be created.')
    return render(request, 'register.html', {'form': form, 'sp_form': sp_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:
            auth.login(request, user)
            data = SpecialUser.objects.all()
            dt = {"all": data}
            return render(request, "profilepage.html", dt)
        else:
            messages.info(request, 'invalid credentials')
            return redirect('login')
    else:
        return render(request, 'login.html')


def logout(request):
    auth.logout(request)
    return redirect('login')

profilepage.html profilepage.html

<h1>{{user.username}}</h1>
<h4>Email : {{user.email}}</h4>
<h5>Phone Number : {{all.phonenumber}}</h5>
{%if user.flag %}
<button>Special User</button>
{%endif%}

here is the correct html you need to see your data这是您需要查看数据的正确 html

    {%for d in all%}    
    {%ifequal d.user user%}
    <h1>{{d.user.username}}</h1>
    <h4>Email : {{d.user.email}}</h4>

      <h5>Phone Number : {{d.phonenumber}}</h5>
      {%if d.flag %}
        <button>Special User</button>
      {%endif%}
    {%endifequal%}
    {%endfor%}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Django Forms 在前端显示 model 字段的动态且可靠的值列表? - How do I show dynamic and dependable list of values of model field on Front End using Django Forms? 如何在我想要的 html 页面中显示 Django 模型? - How can I show the Django model in the html page I want? 如何根据 Django 中的 model 字段显示导航栏项目? - How do I show navbar items based on a model field in Django? 如何在HTML页面上显示模型字段-Django - How to display model field on html page - Django 如何在 Django 的子 model 字段中使用抽象 model 字段值作为默认值? - How do I use abstract model field values as default values in child model fields in Django? 如何让 Django 在模板中显示来自模型的图像字段? - How Do I Get Django to Show Image Field From Model in Templates? 如何在 html django 中过滤模型 - How do I filter a model in html django 如何使表单的选择字段值显示页面上的实际值而不是数字? - How do I make a form's Choice Field values show the actual values on a page instead of numbers? 我想将django模型表示为html页中的表,表标题与django字段名称相同。 我该如何实施? - I want to represent django model as table in html page with table heading same as django field name. How can I implement this? 如何显示[PostgreSQL]数据库中[HTML]中[Django]模型的结果? - How do I show the results of my [Django] model in [HTML] from a [PostgreSQL] database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM