并排显示两种形式Django-Crispy

时间:2016-05-17 03:01:17

标签: django forms django-crispy-forms

我正在尝试创建一个模式,您在右侧有一个登录表单,左侧有一个注册表单。到目前为止,我已经将注册表单显示在模态中,但是我很难找到一种方法让登录表单显示在它旁边(相反,我得到两个注册表单显示在彼此旁边。)Crispy表格是否允许这样的东西?(同一模态中的两种形式)

这是我的代码:

forms.py

from django import forms
from apps.accounts.models import CustomUser
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit, Field, Div


class AuthenticationForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput, label='username')
    password = forms.CharField(widget=forms.PasswordInput, label='password')

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'POST'
        self.helper.add_input(Submit('register', 'Register', css_class='btn btn-primary'))
        self.helper.form_id = 'Crispy_LoginForm'
        super(AuthenticationForm, self).__init__(*args, **kwargs)

    class Meta:
        fields = ['username', 'password'] 


class RegistrationForm(forms.ModelForm):
    first_name = forms.CharField(widget=forms.TextInput, label='First Name')
    last_name = forms.CharField(widget=forms.TextInput, label='Last Name')
    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')

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'POST'
        self.helper.add_input(Submit('register', 'Register', css_class='btn btn-primary'))
        self.helper.form_id = 'Crispy_RegistrationForm'
        super(RegistrationForm, self).__init__(*args, **kwargs)

    class Meta:
        model = CustomUser
        fields = ['first_name', 'last_name', 'email', 'username', 'password1', 'password2']

views.py

def login(request):
    """
    User Log in View
    """
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            user = authenticate(username=request.POST['username'], password=request.POST['password'])
            if user is not None:
                if user.is_active:
                    django_login(request, user)
                    return redirect('all_posts.html')
    else:
        form = AuthenticationForm()
    return render_to_response('register.html', {
        'form': form,
    }, context_instance=RequestContext(request))


def logout(request):
    """
    Log out view
    """
    django_logout(request)
    return redirect('/')


def register(request):
    form = RegistrationForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            CustomUser = form.save()
            CustomUser.save()
            return redirect('all_posts.html')
    else:
        return render(request, 'register.html', {'form': form})

def all_posts(request):
   post_list = TextPost.objects.all().order_by('-popularity')
   paginator = Paginator(post_list, 100) # Show 100 contacts per page
   form = RegistrationForm(request.POST or None)

   page = request.GET.get('page')

   try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        posts = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        posts = paginator.page(paginator.num_pages)

    return render(request, 'all_posts.html', {'form': form})

register.html

{% load staticfiles %}
{% load crispy_forms_tags %}


{% block body %}

<HEAD>
<style>

.modal-dialog {
    width: 808px;
}
.left-div {
    float: left;
    width: 392px;
    padding-left: 8px;
}
.right-div {
    width: 400px;
    padding-left: 8px;
    margin-left: 400px;
    border-left-style: inset;
}​​
</style>
</HEAD>

<div class="modal" id="modal-1">
            <div class="modal-dialog" id="box1">
                <div class="modal-content">
                    <div class="left-div">
                    <form enctype="multipart/form-data" method="post" action="{{ register }}" id='Crispy_RegistrationForm'>
                        <div class="modal-header">
                            <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="Sign up" />
                        </div>
                    </form>
                    </div>
                    <div class="right-div">
                    <form enctype="multipart/form-data" method="post" action="{{ login }}" id='Crispy_LoginForm'>
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h3 class="modal-title">Log In</h3>
                        </div>
                        <div class="modal-body">
                            {% csrf_token %}
                            {{ form|crispy }}
                        </div>
                        <div class="modal-footer">
                            <input type='submit' class="btn btn-default" value="Log in" />
                        </div>
                    </form>
                    </div>
                </div>
            </div>
</div>

{% endblock %}

1 个答案:

答案 0 :(得分:1)

您已在上下文中声明了一个表单,并在html中使用了两次。

请参阅此链接以了解有关多种表单的更多信息 Proper way to handle multiple forms on one page in Django