Reputation: 1911
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>
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'
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>')
Upvotes: 0
Views: 255
Reputation: 599480
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.
Upvotes: 1