[英]Choice field in Django registration forms look inactive, but works
So the thing is that I added custom user model所以问题是我添加了自定义用户 model
models.py
class CustomUserModel(AbstractUser):
pass
Morasko = "Morasko"
Piatkowo = "Piątkowo"
district_choices = [
(Morasko, "Morasko"),
(Piatkowo, "Piątkowo"),
]
district = models.CharField(max_length=15, choices=district_choices, default=district_choices[0])
Field 'district' is supposed to be a choice field with two options.字段“区”应该是一个有两个选项的选择字段。 Here is custom registration form I am using.这是我正在使用的自定义注册表单。
forms.py
class NewUserForm(UserCreationForm):
district = forms.ChoiceField(widget=forms.RadioSelect, choices=CustomUserModel.district_choices)
class Meta(UserCreationForm):
model = CustomUserModel
fields = ('username', 'email', 'district', 'password1', 'password2')
def save(self, commit=True):
user = super(NewUserForm, self).save(commit=False)
user.email = self.cleaned_data['email']
user.district = self.cleaned_data['district']
if commit:
user.save()
return user
My html registration file:我的 html 注册文件:
register.html
{% extends "map_neigh/home.html" %}
{% block content %}
<div class='container'>
<div id='inner'>
<br>
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<button class="btn btn-primary" type="submit">Register</button>
</form>
<br>
<br>
If you already have an account <a href="/login"><strong>log in.</strong></a>
</div>
</div>
{% endblock %}
Choice field actually works - users are saved in DB with clicked district, but it looks inactive and I have no clue why.选择字段实际上有效 - 用户通过点击区保存在数据库中,但它看起来不活跃,我不知道为什么。 Clicking it doesn't change the appearance, if I hove over one of options pointer doesn't change neither.单击它不会改变外观,如果我将鼠标悬停在选项指针之一上也不会改变。 Below screenshot of the registration form.下面是报名表截图。
home.html
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>NoFence</title>
{% load static %}
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<link rel = "stylesheet" type = "text/css" href = "{% static 'style.css' %}"/>
<!--bootstrap-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!--jQuery links -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
<!--leaflet links -->
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet.locatecontrol@0.65.2/dist/L.Control.Locate.min.css" />
<script src="https://cdn.jsdelivr.net/npm/leaflet.locatecontrol@0.65.2/src/L.Control.Locate.min.js" charset="utf-8"></script>
<!--toast-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<!--jquery scripts -->
<script>
</script>
</head>
<body>
<!--navigation bar-->
{% include "map_neigh/includes/navbar.html" %}
<!--user messages-->
{% include "map_neigh/includes/messaging.html" %}
{% block content %}
{% endblock %}
</body>
</html>
style.css
body {
margin:0;
padding:0;
background-color:#808080;
}
#menu_res {
position: relative;
padding-left: 15%;
font-size: 1.25vw;
}
#menu {
position: relative;
padding-left: 15%;
padding-top: 1.5vw;
padding-bottom: 0.9vw;
font-size: 1.5vw;
}
html{
scroll-behavior: smooth;
}
#inner {
width: 50%;
margin: 0 auto;
}
Try this尝试这个
models.py模型.py
class CustomUserModel(AbstractUser):
Morasko = "Morasko"
Piatkowo = "Piątkowo"
district_choices = (
(Morasko, "Morasko"),
(Piatkowo, "Piątkowo"),
)
district = models.CharField(
max_length=15,
choices=district_choices,
default=Morasko)
In your forms.py, your UserCreationForm needs to be a ModelForm, or you can just directly define it like this.在您的 forms.py 中,您的 UserCreationForm 需要是 ModelForm,或者您可以直接像这样定义它。 forms.py forms.py
class NewUserForm(forms.ModelForm):
class Meta:
model = CustomerUserModel
fields = ('username', 'email', 'district', 'password1', 'password2')
widgets = {
'district': forms.RadioSelect,
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.