我一直在努力解决这个问题超过一周,但没有成功。我有一个注册表格,我想把它放在一个模态中。模态渲染得很好,标题,提交按钮,但注册表单本身没有显示。
然而,当我在我的浏览器中访问mysite /注册时,注册表单呈现完美....但我不希望它在一个单独的选项卡中自行呈现,我需要一个模态。< / p>
非常感谢任何帮助。
这是我的代码:
forms.py
from django import forms
from apps.accounts.models import CustomUser
class RegistrationForm(forms.ModelForm):
email = forms.EmailField(widget=forms.TextInput, label='email')
username = forms.CharField(widget=forms.TextInput, label='username')
password1 = forms.CharField(widget=forms.PasswordInput, label='Enter your password')
password2 = forms.CharField(widget=forms.PasswordInput, label='Re-type your password')
first_name = forms.CharField(widget=forms.TextInput, label='First Name')
last_name = forms.CharField(widget=forms.TextInput, label='Last Name')
class Meta:
model = CustomUser
fields = ['email', 'username', 'password1', 'password2', 'first_name', 'last_name']
def clean(self, password1, password2):
cleaned_data = super(RegistrationForm, self).clean()
if password1 in self.cleaned_data and password2 in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError("Passwords don't match. Please enter both fields again")
return self.cleaned_data
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user
views.py
def register(request):
"""
User Registration View
"""
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
CustomUser = form.save()
CustomUser.save()
return redirect('home.html')
else:
form = RegistrationForm()
return render_to_response('register.html', {
'form': form,
}, context_instance=RequestContext(request))
def get_profile(request, username):
user = CustomUser.objects.get(pk=1).username
return render_to_response(request, 'profile.html', {'username': username, 'user': user})
urls.py
urlpatterns = patterns ('',
url(r'register$', 'apps.accounts.views.register', name='register'),
url(r'^profile/(?P<username>\w+/$)', get_profile, name='profile'),
)
register.html
{% load staticfiles %}
{% load crispy_forms_tags %}
{% block body %}
<div class="modal" id="modal-1">
<div class="modal-dialog">
<div class="modal-content">
<form enctype="multipart/form-data" method="post" action="">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Sign-up</h3>
</div>
<div class="modal-body">
{% csrf_token %}
{{ form|crispy }}
</div>
<div class="modal-footer">
<input type='submit' class="btn btn-primary" value="Register" />
</div>
</form>
</div>
</div>
</div>
{% endblock %}
base.html文件
.....
<li><a data-toggle="modal" data-target="#modal-1" href="">sign up</a></li>
{% include 'register.html' %}
答案 0 :(得分:1)
不确定你的帖子中的缩进是否是你在views.py文件中的缩进,但至少这里是错误的并且表格没有呈现。
试试这个:
def register(request):
form = RegistrationForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
CustomUser = form.save()
CustomUser.save()
return redirect('home.html')
else:
return render(request, 'register.html', {'form': form})