0

I have designed an sample registration form and was using it save it to database . I can successfully save an entry to database but the problem is when create an second entry it overrides the first entry

register.html

<form action="/register_process/" method="post">
{% csrf_token %}
<label for="unamefield">Enter Username</label>
<input type="text" name="unamefield">
<label for="unamefield">Enter Password</label>
<input type="password" name="pwdfield">
<input type="submit" value="Register">
</form>

models.py

from django.db import models

class UserRegistration(models.Model):
    USER_ID = models.CharField(primary_key=True, max_length=11)
    USER_NAME = models.CharField(max_length=50)
    PASSWORD = models.CharField(max_length=255, null=False)

    class Meta:
        db_table = 'USER'

views.py

def register_user(request):
    args = {}
    args.update(csrf(request))
    return render_to_response('register.html', args)


def register_process(request):
    if request.method == 'POST':

        uname = request.POST.get('unamefield')
        pwd = request.POST.get('pwdfield')
        obj_userregistration = UserRegistration(USER_NAME=uname, PASSWORD=pwd)
        obj_userregistration.save()
        return HttpResponseRedirect('<html>Success</html>')
2
  • What do you mean overrides? Did you make a SELECT from database to see what it has? Commented May 23, 2014 at 9:17
  • yea i used SELECT * FROM MYSITE.USER; The previously created entry in overwritten by the current entry Commented May 23, 2014 at 9:37

1 Answer 1

1

You've made a few errors in defining your model.

First, there is no reason to define your fields IN UPPER CASE. That's very strange.

More importantly, you have defined a USER_ID field as a charfield and set it to be the primary key. But you have not provided any way to actually generate a new value for this field. Unless you have a really good reason, you should not define a manual PK field at all, but let Django add an autoincremented integer field automatically.

But even more importantly than this, you should on no account do what you have done here at all. You are storing your passwords in clear text in the database, opening yourself to all sorts of hacking. Do not ever do this. Django includes a whole authentication framework where this is done properly, and there is absolutely no reason for you to bypass this as you have done. Do not do it.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay . I am just practicing Django . I am an beginner so very confused where to start and go . Thanks i will follow your steps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.