gpt4 book ai didi

html - 在 Django 中设置自定义用户注册表单的样式

转载 作者:行者123 更新时间:2023-11-27 23:58:11 24 4
gpt4 key购买 nike

我已经创建了自定义用户注册。我想使用输入占位符和类名来设置它们的样式。

模型.py

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
username=models.CharField(max_length=30, unique=True)
email=models.EmailField()
password1=models.CharField(max_length=30)
highestQualification=models.CharField(max_length=50)


def __str__(self):
return self.email

这就是我想要使用输入字段样式和占位符来设置我的 signup.html 样式的方式。

<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
}

input{
padding: 12px;
border: 1px solid gray;
}

input:hover{
opacity: 1;
}
</style>
</head>

<body>
<div class="container">
<form method="post">
<div class="row">
<div class="col">

<input type="text" name="username" placeholder="Username" required>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password1" placeholder="Password" required>
<input type="text" name="highestQualification" placeholder="Highest qualification" required>
<input type="submit" value="Sign up">
</div>
</div>
</form>
</div>
</body>
</html>

注册表单的基本版本(没有样式)如下所示。注册.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

{% block title %}Sign Up{% endblock %}

{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign up</button>
</form>
{% endblock %}
</head>
<body>

</body>
</html>

想知道在哪里提及样式类名称和输入字段的占位符。

最佳答案

您可以传递小部件中的所有属性。

 name = forms.CharField(
label=_('Person Name'),
required=True,
error_messages={
'required': _('Please Enter Person Name!')
},
widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': _('Enter Person Name'),
'data-msg': _('Starred fields must be filled.')})
)



email = forms.EmailField(
label=_('Person Email'),
required=True,
error_messages={
'required': _('Please Enter Person Email!')
},
widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': _('Enter Person Email'),
'data-msg': _('Starred fields must be filled.')})
)

new_password = forms.CharField(
required=True,
widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': _('Enter New Password'),
'data-msg': _('Starred fields must be filled.')}),
label=_('New Password'),
error_messages={
'required': _('New Password is required!')
}
)

关于html - 在 Django 中设置自定义用户注册表单的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56146786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com