Re: While registering the user with django form, it gives "UNIQUE constraint failed: auth_user.username" error

2019-06-05 Thread Chetan Ganji
I wrote a python script to easily reset a django project. I hope it helps
you.
Just put it in the folder where manage.py file is and run it.


import os
import shutil

from pprint import pprint


folders = []
base_dir = os.path.dirname(os.path.realpath(__file__))


def get_directory_list():
global folders
global base_dir

for root, d_names, f_names in os.walk(base_dir):
for name in d_names:
folders.append(os.path.join(root, name))
folders = sorted(folders)
return folders


def delete_pycache():
global folders

for folder in folders:
if folder.endswith("__pycache__"):
shutil.rmtree(folder)

print("All __pycache__ files deleted.")
return None


def delete_migrations():
global folders

for folder in folders:
if folder.endswith("migrations"):
for item in os.listdir(folder):
if not item.endswith("__init__.py"):
os.remove(os.path.join(folder, item))

print("All migration files deleted.")
return None


def delete_sqlite3():
global base_dir
db_file = os.path.join(base_dir, "default.sqlite3")
if os.path.exists(db_file):
os.remove(db_file)


def main():
global folders

try:
get_directory_list()
delete_pycache()
delete_migrations()
delete_sqlite3()
print("All operations performed successfully.")
except Exception as e:
print("There was some error")


if __name__ == "__main__":
main()


Regards,
Chetan Ganji
+91-900-483-4183
ganji.che...@gmail.com
http://ryucoder.in


On Wed, Jun 5, 2019 at 6:29 PM Chandrashekhar Singh <
chandrashekhar...@gmail.com> wrote:

> Better will be, delete all migrations, and again migrate
>
> On Fri, May 17, 2019, 12:51 PM RAJENDRA MYTHILI 17BIS0120 <
> rajendra.mythili2...@vitstudent.ac.in> wrote:
>
>> I'm facing the same issue ... Did you figure out what's wrong?
>>
>> On Saturday, October 6, 2018 at 6:28:09 AM UTC-7, 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.py
>>> from django import forms
>>> from django.contrib.auth.models import User
>>> from volunteer.models import UserProfileInfo
>>>
>>> class UserForm(forms.ModelForm):
>>>
>>> class Meta():
>>> model = User
>>> fields = ('email','first_name','last_name')
>>>
>>>
>>>
>>>
>>> views.py
>>> from django.shortcuts import render
>>> from volunteer.forms import UserForm
>>>
>>>
>>>
>>> def register(request):
>>>
>>> registered = False
>>>
>>> if request.method =="POST" :
>>> user_form = UserForm(data=request.POST)
>>>
>>> if user_form.is_valid():
>>>
>>> user = user_form.save()
>>> user.save()
>>>
>>> registered = True
>>>
>>> else:
>>> print(user_form.errors)
>>>
>>> else:
>>> user_form = UserForm()
>>>
>>> return render(request, 'volunteer/volunteer.html',
>>>  {'user_form':user_form,
>>>   'registered':registered})
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> models.py
>>> from django.db import models
>>> from django.contrib.auth.models import User
>>>
>>> class UserProfileInfo(models.Model):
>>>
>>> user=models.OneToOneField(User)
>>>
>>> def __str__(self):
>>> return self.user.first_name
>>> return self.user.last_name
>>> return self.user.email
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> urls.py
>>> from django.conf.urls import url
>>>
>>> from . import views
>>>
>>> app_name = 'volunteer'
>>>
>>> urlpatterns = [
>>>
>>>  url(r'^', views.register, name='register'),
>>> ]
>>>
>>>
>>>
>>> admin.py
>>> from django.contrib import admin
>>> from volunteer.models import UserProfileInfo
>>>
>>> # Register your models here.
>>> admin.site.register(UserProfileInfo)
>>>
>>>
>>>
>>> volunteer.html file which has the user form
>>> 
>>> 
>>> 
>>> 
>>>
>>>
>>> 
>>>
>>>
>>> https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css;>
>>> https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
>>> ">
>>> https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js
>>> ">
>>>  https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js
>>> ">
>>>
>>> 
>>>
>>> 
>>>
>>>  
>>> {% if registered %}
>>>Thanks!
>>> {% else %}
>>>   Register
>>>
>>>
>>>  
>>> {% csrf_token %}
>>> {{ user_form.as_p }}
>>>  
>>>
>>>  {% endif %}
>>>
>>>  
>>>
>>>
>>>  
>>>
>>> 
>>> 
>>>
>>>
>>>
>>> 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" 

Re: While registering the user with django form, it gives "UNIQUE constraint failed: auth_user.username" error

2019-06-05 Thread Chandrashekhar Singh
Better will be, delete all migrations, and again migrate

On Fri, May 17, 2019, 12:51 PM RAJENDRA MYTHILI 17BIS0120 <
rajendra.mythili2...@vitstudent.ac.in> wrote:

> I'm facing the same issue ... Did you figure out what's wrong?
>
> On Saturday, October 6, 2018 at 6:28:09 AM UTC-7, 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.py
>> from django import forms
>> from django.contrib.auth.models import User
>> from volunteer.models import UserProfileInfo
>>
>> class UserForm(forms.ModelForm):
>>
>> class Meta():
>> model = User
>> fields = ('email','first_name','last_name')
>>
>>
>>
>>
>> views.py
>> from django.shortcuts import render
>> from volunteer.forms import UserForm
>>
>>
>>
>> def register(request):
>>
>> registered = False
>>
>> if request.method =="POST" :
>> user_form = UserForm(data=request.POST)
>>
>> if user_form.is_valid():
>>
>> user = user_form.save()
>> user.save()
>>
>> registered = True
>>
>> else:
>> print(user_form.errors)
>>
>> else:
>> user_form = UserForm()
>>
>> return render(request, 'volunteer/volunteer.html',
>>  {'user_form':user_form,
>>   'registered':registered})
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> models.py
>> from django.db import models
>> from django.contrib.auth.models import User
>>
>> class UserProfileInfo(models.Model):
>>
>> user=models.OneToOneField(User)
>>
>> def __str__(self):
>> return self.user.first_name
>> return self.user.last_name
>> return self.user.email
>>
>>
>>
>>
>>
>>
>>
>> urls.py
>> from django.conf.urls import url
>>
>> from . import views
>>
>> app_name = 'volunteer'
>>
>> urlpatterns = [
>>
>>  url(r'^', views.register, name='register'),
>> ]
>>
>>
>>
>> admin.py
>> from django.contrib import admin
>> from volunteer.models import UserProfileInfo
>>
>> # Register your models here.
>> admin.site.register(UserProfileInfo)
>>
>>
>>
>> volunteer.html file which has the user form
>> 
>> 
>> 
>> 
>>
>>
>> 
>>
>>
>> https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css;>
>> https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
>> ">
>> https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js
>> ">
>>  https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js
>> ">
>>
>> 
>>
>> 
>>
>>  
>> {% if registered %}
>>Thanks!
>> {% else %}
>>   Register
>>
>>
>>  
>> {% csrf_token %}
>> {{ user_form.as_p }}
>>  
>>
>>  {% endif %}
>>
>>  
>>
>>
>>  
>>
>> 
>> 
>>
>>
>>
>> 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+unsubscr...@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/c59e6938-86fd-4cfc-90ed-3053032b8e2d%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@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/CADNvQ1OkbZvJNeej3nVzKdt0UnyUSG2ts%3DvirLc1Y-VVbq9c5w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: While registering the user with django form, it gives "UNIQUE constraint failed: auth_user.username" error

2019-06-05 Thread Joe Reitman
The problem with Jaydeep's code is the 'username' field is not getting 
populated in the auth_user table. Django auth_user table by default must 
have unique usernames. Username is what Django authenticates to by default. 

Jaydeep was able to enter the initial user with email, firstname and 
lastname once because he left 'username' field blank. The second time 
failed because there is already a blank 'username' causing it to throw the 
Unique Constraint error. 

Also, as this code is written the 'userprofileinfo' table is not getting 
populated with any data.



On Tuesday, June 4, 2019 at 8:24:30 AM UTC-5, Flexian_me wrote:
>
> 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:
>>
>> 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.py 
>>> from django import forms
>>> from django.contrib.auth.models import User
>>> from volunteer.models import UserProfileInfo
>>>
>>> class UserForm(forms.ModelForm):
>>>
>>> class Meta():
>>> model = User
>>> fields = ('email','first_name','last_name')
>>>
>>>
>>>
>>>
>>> views.py 
>>> from django.shortcuts import render
>>> from volunteer.forms import UserForm
>>>
>>>  
>>>
>>> def register(request):
>>>
>>> registered = False
>>>
>>> if request.method =="POST" :
>>> user_form = UserForm(data=request.POST)
>>>
>>> if user_form.is_valid():
>>>
>>> user = user_form.save()
>>> user.save()
>>>
>>> registered = True
>>>
>>> else:
>>> print(user_form.errors)
>>>
>>> else:
>>> user_form = UserForm()
>>>
>>> return render(request, 'volunteer/volunteer.html',
>>>  {'user_form':user_form,
>>>   'registered':registered})
>>>
>>>  
>>>   
>>>
>>>
>>> 
>>>
>>>
>>>
>>> models.py 
>>> from django.db import models
>>> from django.contrib.auth.models import User
>>>
>>> class UserProfileInfo(models.Model):
>>>
>>> user=models.OneToOneField(User)
>>>
>>> def __str__(self):
>>> return self.user.first_name
>>> return self.user.last_name
>>> return self.user.email
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> urls.py
>>> from django.conf.urls import url
>>>
>>> from . import views
>>>
>>> app_name = 'volunteer'
>>>
>>> urlpatterns = [
>>>
>>>  url(r'^', views.register, name='register'),
>>> ]
>>>
>>>
>>>
>>> admin.py
>>> from django.contrib import admin
>>> from volunteer.models import UserProfileInfo
>>>
>>> # Register your models here.
>>> admin.site.register(UserProfileInfo)
>>>
>>>
>>>
>>> volunteer.html file which has the user form
>>> 
>>> 
>>> 
>>> 
>>>  
>>>  
>>> 
>>>
>>>  
>>> https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css;>
>>> https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
>>> ">
>>> https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js
>>> ">
>>>  https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js
>>> ">
>>>  
>>>   
>>>  
>>>   
>>>
>>>  
>>> {% if registered %}
>>>Thanks!
>>> {% else %}
>>>   Register
>>>
>>>
>>>  
>>> {% csrf_token %}
>>> {{ user_form.as_p }}
>>>  
>>>
>>>  {% endif %}
>>>
>>>   
>>>
>>>  
>>>  
>>>
>>> 
>>>  
>>>
>>>
>>>
>>> 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+unsubscr...@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 

Re: While registering the user with django form, it gives "UNIQUE constraint failed: auth_user.username" error

2019-06-04 Thread Flexian_me
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:
>
> 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.py 
>> from django import forms
>> from django.contrib.auth.models import User
>> from volunteer.models import UserProfileInfo
>>
>> class UserForm(forms.ModelForm):
>>
>> class Meta():
>> model = User
>> fields = ('email','first_name','last_name')
>>
>>
>>
>>
>> views.py 
>> from django.shortcuts import render
>> from volunteer.forms import UserForm
>>
>>  
>>
>> def register(request):
>>
>> registered = False
>>
>> if request.method =="POST" :
>> user_form = UserForm(data=request.POST)
>>
>> if user_form.is_valid():
>>
>> user = user_form.save()
>> user.save()
>>
>> registered = True
>>
>> else:
>> print(user_form.errors)
>>
>> else:
>> user_form = UserForm()
>>
>> return render(request, 'volunteer/volunteer.html',
>>  {'user_form':user_form,
>>   'registered':registered})
>>
>>  
>>   
>>
>>
>> 
>>
>>
>>
>> models.py 
>> from django.db import models
>> from django.contrib.auth.models import User
>>
>> class UserProfileInfo(models.Model):
>>
>> user=models.OneToOneField(User)
>>
>> def __str__(self):
>> return self.user.first_name
>> return self.user.last_name
>> return self.user.email
>>
>>
>>
>>
>>
>>
>>
>> urls.py
>> from django.conf.urls import url
>>
>> from . import views
>>
>> app_name = 'volunteer'
>>
>> urlpatterns = [
>>
>>  url(r'^', views.register, name='register'),
>> ]
>>
>>
>>
>> admin.py
>> from django.contrib import admin
>> from volunteer.models import UserProfileInfo
>>
>> # Register your models here.
>> admin.site.register(UserProfileInfo)
>>
>>
>>
>> volunteer.html file which has the user form
>> 
>> 
>> 
>> 
>>  
>>  
>> 
>>
>>  
>> https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css;>
>> https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
>> ">
>> https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js
>> ">
>>  https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js
>> ">
>>  
>>   
>>  
>>   
>>
>>  
>> {% if registered %}
>>Thanks!
>> {% else %}
>>   Register
>>
>>
>>  
>> {% csrf_token %}
>> {{ user_form.as_p }}
>>  
>>
>>  {% endif %}
>>
>>   
>>
>>  
>>  
>>
>> 
>>  
>>
>>
>>
>> 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+unsubscr...@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.


Re: While registering the user with django form, it gives "UNIQUE constraint failed: auth_user.username" error

2019-05-17 Thread Joe Reitman
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.py 
> from django import forms
> from django.contrib.auth.models import User
> from volunteer.models import UserProfileInfo
>
> class UserForm(forms.ModelForm):
>
> class Meta():
> model = User
> fields = ('email','first_name','last_name')
>
>
>
>
> views.py 
> from django.shortcuts import render
> from volunteer.forms import UserForm
>
>  
>
> def register(request):
>
> registered = False
>
> if request.method =="POST" :
> user_form = UserForm(data=request.POST)
>
> if user_form.is_valid():
>
> user = user_form.save()
> user.save()
>
> registered = True
>
> else:
> print(user_form.errors)
>
> else:
> user_form = UserForm()
>
> return render(request, 'volunteer/volunteer.html',
>  {'user_form':user_form,
>   'registered':registered})
>
>  
>   
>
>
> 
>
>
>
> models.py 
> from django.db import models
> from django.contrib.auth.models import User
>
> class UserProfileInfo(models.Model):
>
> user=models.OneToOneField(User)
>
> def __str__(self):
> return self.user.first_name
> return self.user.last_name
> return self.user.email
>
>
>
>
>
>
>
> urls.py
> from django.conf.urls import url
>
> from . import views
>
> app_name = 'volunteer'
>
> urlpatterns = [
>
>  url(r'^', views.register, name='register'),
> ]
>
>
>
> admin.py
> from django.contrib import admin
> from volunteer.models import UserProfileInfo
>
> # Register your models here.
> admin.site.register(UserProfileInfo)
>
>
>
> volunteer.html file which has the user form
> 
> 
> 
> 
>  
>  
> 
>
>  
> https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css;>
> https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
> ">
> https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js
> ">
>  https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js
> ">
>  
>   
>  
>   
>
>  
> {% if registered %}
>Thanks!
> {% else %}
>   Register
>
>
>  
> {% csrf_token %}
> {{ user_form.as_p }}
>  
>
>  {% endif %}
>
>   
>
>  
>  
>
> 
>  
>
>
>
> 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+unsubscr...@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/ec026d1c-9724-4826-b187-cd80ff30744a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: While registering the user with django form, it gives "UNIQUE constraint failed: auth_user.username" error

2019-05-17 Thread Guru Murthy
Hi,
Your _str function name is only one name is give in that model

Regards
Gurumurthy P


On Fri, 17 May, 2019, 10:21 PM RAJENDRA MYTHILI 17BIS0120, <
rajendra.mythili2...@vitstudent.ac.in> wrote:

> I'm facing the same issue ... Did you figure out what's wrong?
>
> On Saturday, October 6, 2018 at 6:28:09 AM UTC-7, 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.py
>> from django import forms
>> from django.contrib.auth.models import User
>> from volunteer.models import UserProfileInfo
>>
>> class UserForm(forms.ModelForm):
>>
>> class Meta():
>> model = User
>> fields = ('email','first_name','last_name')
>>
>>
>>
>>
>> views.py
>> from django.shortcuts import render
>> from volunteer.forms import UserForm
>>
>>
>>
>> def register(request):
>>
>> registered = False
>>
>> if request.method =="POST" :
>> user_form = UserForm(data=request.POST)
>>
>> if user_form.is_valid():
>>
>> user = user_form.save()
>> user.save()
>>
>> registered = True
>>
>> else:
>> print(user_form.errors)
>>
>> else:
>> user_form = UserForm()
>>
>> return render(request, 'volunteer/volunteer.html',
>>  {'user_form':user_form,
>>   'registered':registered})
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> models.py
>> from django.db import models
>> from django.contrib.auth.models import User
>>
>> class UserProfileInfo(models.Model):
>>
>> user=models.OneToOneField(User)
>>
>> def __str__(self):
>> return self.user.first_name
>> return self.user.last_name
>> return self.user.email
>>
>>
>>
>>
>>
>>
>>
>> urls.py
>> from django.conf.urls import url
>>
>> from . import views
>>
>> app_name = 'volunteer'
>>
>> urlpatterns = [
>>
>>  url(r'^', views.register, name='register'),
>> ]
>>
>>
>>
>> admin.py
>> from django.contrib import admin
>> from volunteer.models import UserProfileInfo
>>
>> # Register your models here.
>> admin.site.register(UserProfileInfo)
>>
>>
>>
>> volunteer.html file which has the user form
>> 
>> 
>> 
>> 
>>
>>
>> 
>>
>>
>> https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css;>
>> https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
>> ">
>> https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js
>> ">
>>  https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js
>> ">
>>
>> 
>>
>> 
>>
>>  
>> {% if registered %}
>>Thanks!
>> {% else %}
>>   Register
>>
>>
>>  
>> {% csrf_token %}
>> {{ user_form.as_p }}
>>  
>>
>>  {% endif %}
>>
>>  
>>
>>
>>  
>>
>> 
>> 
>>
>>
>>
>> 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+unsubscr...@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/c59e6938-86fd-4cfc-90ed-3053032b8e2d%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@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/CAMgkGLVWrHfW08EAY2DWhnKEioyXgOie%3D6vkv-Vr9oSVhGG%3Dsw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: While registering the user with django form, it gives "UNIQUE constraint failed: auth_user.username" error

2019-05-17 Thread RAJENDRA MYTHILI 17BIS0120
I'm facing the same issue ... Did you figure out what's wrong?

On Saturday, October 6, 2018 at 6:28:09 AM UTC-7, 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.py 
> from django import forms
> from django.contrib.auth.models import User
> from volunteer.models import UserProfileInfo
>
> class UserForm(forms.ModelForm):
>
> class Meta():
> model = User
> fields = ('email','first_name','last_name')
>
>
>
>
> views.py 
> from django.shortcuts import render
> from volunteer.forms import UserForm
>
>  
>
> def register(request):
>
> registered = False
>
> if request.method =="POST" :
> user_form = UserForm(data=request.POST)
>
> if user_form.is_valid():
>
> user = user_form.save()
> user.save()
>
> registered = True
>
> else:
> print(user_form.errors)
>
> else:
> user_form = UserForm()
>
> return render(request, 'volunteer/volunteer.html',
>  {'user_form':user_form,
>   'registered':registered})
>
>  
>   
>
>
> 
>
>
>
> models.py 
> from django.db import models
> from django.contrib.auth.models import User
>
> class UserProfileInfo(models.Model):
>
> user=models.OneToOneField(User)
>
> def __str__(self):
> return self.user.first_name
> return self.user.last_name
> return self.user.email
>
>
>
>
>
>
>
> urls.py
> from django.conf.urls import url
>
> from . import views
>
> app_name = 'volunteer'
>
> urlpatterns = [
>
>  url(r'^', views.register, name='register'),
> ]
>
>
>
> admin.py
> from django.contrib import admin
> from volunteer.models import UserProfileInfo
>
> # Register your models here.
> admin.site.register(UserProfileInfo)
>
>
>
> volunteer.html file which has the user form
> 
> 
> 
> 
>  
>  
> 
>
>  
> https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css;>
> https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
> ">
> https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js
> ">
>  https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js
> ">
>  
>   
>  
>   
>
>  
> {% if registered %}
>Thanks!
> {% else %}
>   Register
>
>
>  
> {% csrf_token %}
> {{ user_form.as_p }}
>  
>
>  {% endif %}
>
>   
>
>  
>  
>
> 
>  
>
>
>
> 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+unsubscr...@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/c59e6938-86fd-4cfc-90ed-3053032b8e2d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: While registering the user with django form, it gives "UNIQUE constraint failed: auth_user.username" error

2018-10-08 Thread Nelson Varela
You do not have to save the user 2 times in your form:

user = user_form.save()
user.save()

Maybe this is also the problem...

-- 
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+unsubscr...@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/16c6d1c3-4698-4ad4-bcb2-81ff8f1c4f8c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


While registering the user with django form, it gives "UNIQUE constraint failed: auth_user.username" error

2018-10-06 Thread Jaydeep Borkar
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.py 
from django import forms
from django.contrib.auth.models import User
from volunteer.models import UserProfileInfo

class UserForm(forms.ModelForm):

class Meta():
model = User
fields = ('email','first_name','last_name')




views.py 
from django.shortcuts import render
from volunteer.forms import UserForm

 

def register(request):

registered = False

if request.method =="POST" :
user_form = UserForm(data=request.POST)

if user_form.is_valid():

user = user_form.save()
user.save()

registered = True

else:
print(user_form.errors)

else:
user_form = UserForm()

return render(request, 'volunteer/volunteer.html',
 {'user_form':user_form,
  'registered':registered})

 
  






models.py 
from django.db import models
from django.contrib.auth.models import User

class UserProfileInfo(models.Model):

user=models.OneToOneField(User)

def __str__(self):
return self.user.first_name
return self.user.last_name
return self.user.email







urls.py
from django.conf.urls import url

from . import views

app_name = 'volunteer'

urlpatterns = [

 url(r'^', views.register, name='register'),
]



admin.py
from django.contrib import admin
from volunteer.models import UserProfileInfo

# Register your models here.
admin.site.register(UserProfileInfo)



volunteer.html file which has the user form




 
 


 
https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css;>
https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js";>
https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js";>
 https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js";>
 
  
 
  

 
{% if registered %}
   Thanks!
{% else %}
  Register


 
{% csrf_token %}
{{ user_form.as_p }}
 

 {% endif %}

  

 
 


 



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+unsubscr...@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/e74c61d0-db1f-48d3-ac35-f438752275ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.