Consider we want another attribute in user model other than what is already available, for example height or weight. Still this code is giving error and focus should be to solve the issue that OP is facing. I am also facing same issue and trying to figure it out.
-- @Jaydeep Did you get to solve the issue somehow?
Thanks!
On Saturday, 18 May 2019 04:01:56 UTC+5:30, Joe Reitman wrote:
On Saturday, 18 May 2019 04:01:56 UTC+5:30, Joe Reitman wrote:
I'm not sure what your objective is but you don't need to create another model to store user data. User data is stored in the auth.models. It has fields already available for what your trying to accomplish.
On Saturday, October 6, 2018 at 8:28:09 AM UTC-5, Jaydeep Borkar wrote:When I try to register a user using Django form, it gives me "UNIQUE constraint failed: auth_user.username". When I tried registering the first user, it worked, but I couldn't see that entry in my database. When I tried registering the second user, it's giving me this error. Please, help me through this. I have spent a considerable amount of time on this and I'm stuck.This is my code:forms.pyfrom django import formsfrom django.contrib.auth.models import Userfrom volunteer.models import UserProfileInfoclass UserForm(forms.ModelForm):class Meta():model = Userfields = ('email','first_name','last_name') views.pyfrom django.shortcuts import renderfrom volunteer.forms import UserFormdef register(request):registered = Falseif request.method =="POST" :user_form = UserForm(data=request.POST)if user_form.is_valid():user = user_form.save()user.save()registered = Trueelse:print(user_form.errors)else:user_form = UserForm()return render(request, 'volunteer/volunteer.html',{'user_form':user_form,'registered':registered})models.pyfrom django.db import modelsfrom django.contrib.auth.models import Userclass UserProfileInfo(models.Model):user=models.OneToOneField(User) def __str__(self):return self.user.first_namereturn self.user.last_namereturn self.user.emailurls.pyfrom django.conf.urls import urlfrom . import viewsapp_name = 'volunteer'urlpatterns = [url(r'^', views.register, name='register'),]admin.pyfrom django.contrib import adminfrom volunteer.models import UserProfileInfo# Register your models here.admin.site.register(UserProfileInfo) volunteer.html file which has the user form<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content = "width=device-width, initial-scale=1.0"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4. ">1.3/css/bootstrap.min.css <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/ "></script>jquery.min.js <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14. "></script>3/umd/popper.min.js <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4. "></1.3/js/bootstrap.min.js script> </head><body><div class="jumbotron">{% if registered %}<h1>Thanks!</h1>{% else %}<h1>Register</h1><form method="post">{% csrf_token %}{{ user_form.as_p }}<input type="submit" name="" value="Register">{% endif %}</div></form></body></html>I feel there's some problem in views.py or models.py, or volunteer.html. or maybe something else. Please, guide me through this.Thanks in advance.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9c4d4774-96ca-4c03-b63c-963628303544%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment