我有一个Django项目,用户在其中注册(这是使用中的默认用户注册),注册后我希望他们能够选择一个选项-W或WP(单选按钮)。根据他们选择的按钮,它们将被重定向到适当的配置文件页面。 (例如profile-W或profile-WP)
“更多信息”中间页面如下所示(html) more-info page showing two radio button options
我希望获得有关实现此目标的最佳方法的一些指导。我认为前两个步骤是正确的吗?1)为more-info页面创建一个表单2)创建一个将用户重定向到个人资料的视图。到目前为止,我有这个
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
"""
Step 1- Create a More Info form
class MoreInfoForm():
choice = forms.ChoiceField(required=True, widget=forms.RadioSelect(
attrs={'class': 'Radio'}), choices=(('option1','I am a W'),('option2','I am a WP'),))
"""
views.py
"""
STEP 2 - Create another view
def moreinforequest(request):
if request.method =='POST':
form = UserMoreInfoForm(request.POST) #create a form that has the data that was in request.POST
if form.is_valid(): #is the form valid (has something been ticked?)
form.save()#this just saves the account, hashes the password and it's all done for you!
#username = form.cleaned_data.get('username')
messages.success(request,f'Account created for {username}, you can now login.')
return redirect('login')
else:
form =UserMoreInfoForm() #if the form input is invalid, render the empty form again
Pseudocode: If W selected then:
return Profile-W
Else
return Profile-WP
"""
我根本不了解的是如何创建与之相关的其他表单和功能。该示例将大大帮助我创建表单和视图的其余功能。
对于一个答案,我将不胜感激,如果有人可以a)建议实现上述目标的最佳步骤(创建将注册用户定向到个人资料1或个人资料2的中间表格)和两个最佳方法处理单选按钮形式。
任何直接相关且有帮助的教程或链接也将很有用。
moreinfo.html 在下面-我需要通过添加代码来完成代码。
{% extends "socialmedia/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<h1 class="sso-title">Nearly there...</h1>
<p>We need a little information from you so we can route you to the correct profile. It'll just take a few
seconds. Fill in the form below</p>
<div class="container">
<h2>WP or W?</h2>
<p>Please select one of the options below to confirm your identity. W = LFW. WP e.g R, C, CH, H, GH, L, S, etc.</p>
<form>
<div class="radio">
<label><input type="radio" name="optradio" checked>W</label>
</div>
<div class="radio">
<label><input type="radio" name="optradio">WSP</label>
</div>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Confirm</button>
</div>
</form>
</div>
{% endblock content %}
<!-- below I am overriding the sidebar that is generated in the base.html, by adding something
else, in this case, empty p tags, which means the side bar won't show on this page-->
{% block sidebar %}<p></p>{% endblock sidebar %}
<