提交按钮不在django的Modal注册表单中工作

时间:2015-09-05 18:45:42

标签: python django forms form-submit bootstrap-modal

我几乎尝试了所有的东西,并且几天来一直困在这个愚蠢的问题上。所以我想在Django1.8中创建一个Modal弹出登录/注册页面。我已经设法创建了所有内容,除了当我点击我的注册表单的提交按钮时,它不起作用。有谁可以帮助我。以下是我的代码:

model.py

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

    def create_user_callback(sender, instance, **kwargs):
        UserProfile. new = UserProfile.objects.get_or_create(user=instance)
    post_save.connect(create_user_callback, User)

forms.py

 class RegistrationForm(ModelForm):
    username = forms.CharField(help_text="Please Enter a Name")
    email = forms.EmailField(help_text="Please Enter your Email")
    password = forms.CharField(widget=forms.PasswordInput(), help_text="Please Enter a Password")
    password2 = forms.CharField(widget=forms.PasswordInput(), help_text="Verify Password")

    class Meta:
        model = UserProfile
        fields = ('username', 'email', 'password', 'password2')

    def clean_username(self):
        username = self.cleaned_data['username']
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError("That username is already taken, Please try registering by a different username")

    def clean(self):
            if self.cleaned_data['password'] != self.cleaned_data['password2']:
                    raise forms.ValidationError("The passwords did not match.  Please try again.")
            return self.cleaned_data

views.py

def myRegistration(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/profile/')
    if request.method=='POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
                user = User.objects.create_user(username=form.cleaned_data['username'], email = form.cleaned_data['email'], password = form.cleaned_data['password'])
                user.save()
                launchers = user.get_profile()
                launchers.name = form.cleaned_data['name']
                launchers.save()
                return HttpResponseRedirect('/profile/')
        else:
                return render_to_response('index.html', {'form': form}, context_instance=RequestContext(request))
    else:
        '''user is not submitting the form. show them a blank registration form'''
        form=RegistrationForm()
        context = {'form':form}
        return  render_to_response('index.html', context, context_instance=RequestContext(request))

urls.py

(r'^register/$', 'authentication.views.myRegistration'),

的index.html

            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right">
                    <li>### when you click on the bellow link a modal form opens and then registration is done
                        <a class="page-scroll" data-toggle="modal" href="login.html" data-target="#myModal">Register/Login</a> 

                    </li>
                </ul>
            </div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-hidden="true"> ### The code for modal starts here
    <div class="modal-body">
        <ul id="myTab" class="nav nav-tabs">
            <li><a href="#signin" data-toggle="tab">Sign In</a></li> #I have yet to create this view
            <li><a href="#signup" data-toggle="tab">Register</a></li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane fade" id="signup">
                <form id="user_form" class="form-horizontal" action="" method="post"
                        enctype="multipart/form-data">
                    {% csrf_token %}
                        {{ form.as_p }}
                        <div class="control-group">
                        <label class="control-label" for="confirmsignup"></label>
                            <div class="controls">
                                <input id="confirmsignup" type="submit" name="confirmsignup" class="btn btn-success" value="Submit" />
                            </div>
                        </div>
                </form>
            </div>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
</div

我的主要问题是当我点击提交按钮时没有任何反应,也没有信息传递给/ admin。我在Registration Form not submitting in Django中看到了同样的问题,但即使在纠正了该问题的解决方案后,我仍然有这个错误。

有谁能告诉我我哪里出错了。先感谢您。请帮助

1 个答案:

答案 0 :(得分:0)

您的表单操作属性为空,因此在您提交表单时没有任何反应,您应将其更改为您要传递数据的位置