简体   繁体   中英

Submit button not working in the Modal registration form in django

I tried almost everything and have been stuck at this silly problem for days now. So I am trying to create a Modal popup Login/Registration page in Django1.8. I have managed to create everything except for when I click on submit button for my Registration form, it doesn't work. Could anyone please help me. Below is my code:

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

My is when i click on submit button nothing happens and no information is been passed to /admin also. 是,当我单击“提交”按钮时,没有任何反应,也没有任何信息传递给/ admin。 I have seen the same issue in Registration Form not submitting in Django , but even after rectifying the solution in that question, I still have this error.

Could anyone please tell me where i am going wrong. Thank you in advance. Please please please help

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM