开发者

Django modelform is submitted, but data is not saved?

开发者 https://www.devze.com 2023-02-13 12:40 出处:网络
Okay Guys, I have a custom User Registration form, whenever the form is submitted a browser alert pops up saying \"The Page at 127.0.0.1:8000 says: Submitted!\" and nothing happens. No data is saved.

Okay Guys, I have a custom User Registration form, whenever the form is submitted a browser alert pops up saying "The Page at 127.0.0.1:8000 says: Submitted!" and nothing happens. No data is saved. Following is the code of the Index view where i am trying to save the form:

def Index(request):
  """View to serve the index page."""
  regform = models.RegistrationForm()
  loginform = models.LoginForm()
  if request.method == 'POST':
    if 'signup' in request.POST:
      form = models.RegistrationForm(request.POST)
      if form.is_valid():
        form.save()
        message = 'Your Account has been created. You may login now.'
        return shortcuts.render_to_response(
          'index.html', {'message' : message, 'regform' : regform, 'loginform' : loginform})
      else:
        message = 'Error: Please fill in all the fields and try again'
        return shortcuts.render_to_response(
          'index.html', {'regform' : regform, 'message' : message, 'loginform' : loginform})
    elif 'login' in request.POST:
      requser = request.POST['loginemail']
      reqpass = request.POST['loginpass']
      '''check if email exists'''
      emailexist = models.member.objects.filter(Email=requser).count()
      if emailexist == 1:
        exists = True
      else:
        exists = False
      '''if email exists check for password'''
      if exists == True:
        mem = models.member.objects.get(Email=requser)
        if reqpass == mem.Password:
          request.session['member_id'] = mem.id
          return shortcuts.render_to_response(
            'login.html')
        else:
          error = 'You entered an invalid Password, Please try again.'
          return shortcuts.render_to_response(
            'index.html', {'error' : error, 'regform' : regform, 'loginform' : loginform})
      else:
        error = 'That E-mail Address is Not Registered, Please Check the spelling and try again.'
        return shortcuts.render_to_response(
          'index.html', {'regform' : regform, 'loginform' : loginform, 'error' : error})
  else:
    return shortcuts.render_to_response(
      'index.html', {'regform' : regform, 'loginform' : loginform})

Sorry, Here's the Code to the model and registration form

Model:

class member(models.Model):
  """Model to represent a User."""
  First_Name = models.CharField(max_length=100, blank=False)
  Last_Name = models.CharField(max_length=100, blank=False)
  Stage_Name = models.CharField(max_length=100, unique=True, blank=False)
  Account_Type = models.CharField(max_length=200, choices=USERTYPE_CHOICES, blank=False)
  Password = models.CharField(max_length=100, blank=False)
  Email = models.EmailField(max_length=100, blank=False)
  Sex = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=False)
  Birthday = models.CharField(max_length=2, blank=False)

  def __unicode__(self):
  return self.Email

Registration Form:

class RegistrationForm(forms.ModelForm):
  Stage_Name = forms.CharField(label='Username')
  Email = forms.CharField(initial='you@example.com')
  Birthday = forms.CharField(initial='dd/mm/yyyy')
  Password = forms.CharField(widget=forms.PasswordInput)
  class Meta:
    model=member

And here is the template :::::

{% extends 'base.html' %}
{% load login_checkbox %}
{% block topbar %}
<head>
</head>
<body>
  <div id="logo" class="left"><a href="http://www.muzikube.com/"><img src="/static/images/logo.png" alt="Muzikube" width="200" height="52" border="0"/></a></div>
  <div id="login" class="right">
    <form id="loginform" action="." method="post">
      {% for field in loginform %}
        {% if field.field.widget|is_checkbox %}
          <div class="checkbox">{{field}}{{field.label_tag}}<span id="forgotpass"><a href="/forgot/">Can't Sign In?</a></span></div>
        {% else %}
          {{field.label_tag}}{{field}}
        {% endif %}
      {% endfor %}
      <input id="submit" class="submit" "type="submit" name="login" value="In!" />
    </form>
    {% if error %}
      {{error}}
    {% endif %}
  </div>
  <div class="clear"></div>
</body>
{% endblock topbar %}
{% block content %}
<body>
  <div class="left">
    <div id="heading">Connect To Other Musicians</div>
    <div class="subheading">Upload your music, Share Pictures, Share Videos, Make Friends, Get Known !</div>
    <div id="homepageimage"><img src="/static/images/comm.png" alt="Connect to other Musicians Around the world!" width="450" height=""/></div>
    <div class="subheading">Muzikube let's you connect to other people around the world that share same interests as you !</div>
  </div>
 开发者_运维百科 <div id="registrationform" class="right">
    <div id="form-title">Sign Up, It's Free !</div>
    <div id="form-subtitle">Connect to other musicians around the world.</div>
    <div class="border"></div>
    <div class="error">
      {% if message %}
        {{message}}
      {% endif %}
    </div>
    <form id="regform" action="." method="post">
      <table>
        {{ regform.as_table }}
      </table>
      <input id="register-submit" class="submit" type="submit" name="signup" value="Sign Up" />
    </form>
    <div class="border"></div>
  </div>
  <div class="clear"></div>
</body>
{% endblock content %}


It's a little hard to tell from the code above, but I would guess that you get a POST that doesn't have 'login' or 'signup' in (I can't see why it would have those parameters from the code, maybe it's in the html?) - and so nothing is saved or returned from the post request.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号