非常非常新的Django。我正在尝试使用自定义表单输入创建用户注册过程。但是,我对数据库中新用户的持久性存在一些问题。这就是我在forms.py中定义的内容:
class UserRegistrationForm(UserCreationForm):
required_css_class = 'required'
email = forms.EmailField()
first_name = forms.CharField()
last_name = forms.CharField()
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name')
def __init__(self, *args, **kwargs):
super(UserRegistrationForm, self).__init__(*args, **kwargs)
self.fields['username'].widget = TextInput(attrs={'placeholder': 'Username'})
self.fields['username'].required = True
self.fields['username'].error_messages = {'required': 'Please enter your username'}
self.fields['email'].widget = EmailInput(attrs={'placeholder': 'Email'})
self.fields['email'].required = True
self.fields['email'].error_messages = {'required': 'Please enter your email'}
self.fields['first_name'].widget = TextInput(attrs={'placeholder': 'Forename'})
self.fields['first_name'].required = True
self.fields['first_name'].error_messages = {'required': 'Please enter your first_name'}
self.fields['last_name'].widget = TextInput(attrs={'placeholder': 'Surname'})
self.fields['last_name'].required = True
self.fields['last_name'].error_messages = {'required': 'Please enter your last_name'}
self.fields['password1'].widget = PasswordInput(attrs={'placeholder': 'Password'})
self.fields['password1'].required = True
self.fields['password1'].error_messages = {'required': 'Please enter your Password'}
self.fields['password2'].widget = PasswordInput(attrs={'placeholder': 'Confirm password'})
self.fields['password2'].required = True
self.fields['password2'].error_messages = {'required': 'Please confirm your Password'}
我的views.py文件中也有以下内容:
class UserRegistrationView(FormView):
disallowed_url = ''
form_class = UserRegistrationForm
success_url = '/blog'
template_name = 'oauth/user/registration_form.html'
def registration_allowed(self):
return getattr(settings, 'REGISTRATION_OPEN', True)
def register(self, request):
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
new_user = form.save(commit=False)
new_user.set_password(form.cleaned_data['password1'])
new_user.save()
return render(request, 'blog/post/list.html', { 'new_user': new_user })
else:
return render(request, 'oauth/user/registration_form.html', { 'form': form })
def get_success_url(self, user=None):
return super(UserRegistrationView, self).get_success_url()
令人担忧的是:
对我可能出错的任何帮助都将不胜感激!
答案 0 :(得分:0)
您混淆了基于类和基于函数的视图。基于类的视图没有名为" register&#34 ;;的方法。你永远不会被召唤。
实际上,您编写的方法本身就是一个完整的基于函数的视图。从类中提取它,删除function showMenu {
cls;
write-host “my menu”;
write-host “1... Programm1”;
write-host “2... Programm1”;
write-host “3... Programm1”;
write-host “q...exit”;
return read-host "it´s your choice"
}
do {$x = showMenu}
until ($x -eq ‘q’)
参数,并更改urlconf以直接调用该函数,它应该可以工作。
答案 1 :(得分:0)
创建<h1>Nova Proposta</h1>
<%= @id_item %>
<p>
Realizar proposta para:
</p>
<div>
Itens do usuário que receberá a proposta
</div>
<div>
Itens a serem ofertados:
<br>
<%= form_tag("/ofertar_item_proposta") do %>
<p>
<%= submit_tag "Ofertar Item" %>
</p>
<% end %>
</div>
<div>
<%= render "teste"%>
</div>
<div>
<br>
<h2>Itens ofertados</h2>
<%if @itens_ofertados %>
<% @itens_ofertados.each do |item| %>
<ul>
<li><%= item.nome %></li>
<li><%= item.descricao %></li>
<li><%= item.quantidade %></li>
<li><%= item.categoria.nome %></li>
<% if item.img_link %>
<li><%= image_tag(item.img_link, :size => "260x180") %></li>
<% end %>
</ul>
<% end %>
<% end %>
</div>
<div>
<br>
<h2>Itens a serem selecionados</h2>
<%if @itens %>
<% @itens.each do |item| %>
<%= form_tag("/adicionar_item_proposta") do %>
<ul>
<li><%= item.nome %></li>
<li><%= item.descricao %></li>
<li><%= item.quantidade %></li>
<li><%= item.categoria.nome %></li>
<% if item.img_link %>
<li><%= image_tag(item.img_link, :size => "260x180") %></li>
<% end %>
</ul>
<ul>
<%= hidden_field_tag 'id_item', item.id %>
<li><%= submit_tag 'Adicionar a Proposta' %></li>
</ul>
<% end %>
<% end %>
<% end %>
</div>
方法(或将post
的名称更改为register
)并将其添加到您的班级,以便post
知道如何处理您的班级。然后将其作为视图添加到您的urlconf中。
as_view()
然后,在class UserRegistrationView(FormView):
disallowed_url = ''
form_class = UserRegistrationForm
success_url = '/blog'
template_name = 'oauth/user/registration_form.html'
def post(self, request, *arg, **kwargs):
return self.register(request)
def registration_allowed(self):
return getattr(settings, 'REGISTRATION_OPEN', True)
def register(self, request):
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
new_user = form.save(commit=False)
new_user.set_password(form.cleaned_data['password1'])
new_user.save()
return render(request, 'blog/post/list.html', {'new_user': new_user})
else:
return render(request, 'oauth/user/registration_form.html', {'form': form})
def get_success_url(self, user=None):
return super(UserRegistrationView, self).get_success_url()
:
urls
如果您有兴趣将所有表单逻辑保存到一个视图类中,您还可以添加urlpatterns = [
# . . .
url(r'^register_user/$', UserRegistrationView.as_view(), name='register_user'),
# . . .
]
方法并使用它来呈现表单。