I had a login page that I would change it to another login page and I follow these instructions.
I added this code and when I tried to login to my user admin that I added before it sends me to wrong url http://localhost:8050/admin/login/?next=/admin/ and it throws :
RelatedObjectDoesNotExist at /admin/login/
User has no profile
The profile instance is generated on post_save signal, that is, you must save your User at least once after you added that Profile class.
The easiest workaround in your case would be to create a new admin user using python manage.py createsuperuser.
The error seems like, You have a User in DB, but it doesn't have any profile relation now.
Solution:1
Set profile instance for currently existing User's in db through django shell.
1. log into django shell by python manage.py shell
2. run these commands,
from django.contrib.auth.models import User
from myapp.models import Profile
for user in User.objects.all():
Profile.objects.create(user=user)
3. then login to the django admin
Solution:2
Drop your database, (if you are using sqlite, the delete the corresponding file) and migrate and then create a new super user by python manage.py createsuperuser command
Related
I work on django project that migrate from django social auth to python social auth.
Previously new social auth user first name/last name will be saved automatically for first time login.
Now after using python social auth, it's not.
Seems I have to use this setting:
SOCIAL_AUTH_USER_MODEL
but
SOCIAL_AUTH_USER_MODEL = 'django.contrib.auth.models.User'
generate error when invoking runserver:
django.core.management.base.CommandError: One or more models did not validate:
default.usersocialauth: 'user' has a relation with model web.models.User, which has either not been installed or is abstract.
Wanted to try subclassing User model in the project
from django.contrib.auth.models import User
class User(User):
but that is not feasible right now.
Also saving manually the name from response data in custom pipeline is prohibited as well.
Really want to know if there any other solution?
Thanks.
remove SOCIAL_AUTH_USER_MODEL because you are using Django Default User model.
I have an app running and I made a huge mistake, I've deleted a user from the admin site of my Django app before deleting it's UserProfile.
Now, when I try to see the UserProfiles from the admin site, I keep getting an error, for what I've seen it's a record not found exception.
I've got this problem because I cannot use any backup as the latest one is half a month old and all my users will get really mad if they loose all data.
So, how can I do it so I can enter in the UserProfile (It would be best if using the admin site) to delete the UserProfile?
Thanks
Try doing it from the shell!
You need some way to find that UserProfile, do you have an ID of the user or maybe of the UserProfile itself?
python manage.py shell
>>> from your_app_thing.models import UserProfile
>>> user_profile = UserProfile.objects.get(id=<user profile id>)
>>> user_profile.remove()
Alternatively, you could add UserProfile to the admin panel?
from django.contrib import admin
from .models import UserProfile
admin.site.register(UserProfile)
I am trying to set up the email & ADMINS setting and would like to query the User model to get the email addresses & other email credentials so i don't have to give that info to everybody on git hub. If i put the following in the end of my settings file (after database is defined):
from django.contrib.auth.models import User
u1 = User.objects.get(pk__exact=1)
ADMINS = ((u1.username, u1.email))
print ADMINS
I get a successful query and the sever launches, but when i try to launch the app i get various errors due to missing settings depending where in the settings file i put the above code.
From this i gather that you can not query a database in the settings file so where can i put this code so that it populates the ADMINS settings and email settings variables when the server starts up?
I suggest you not use django models in settings.py,
Generally, after settings.py loads, django knows what the database config is.
A more idiomatic practice is add some piece of code at the end of settings.py:
from local_settings import *
or just overwrite ADMINS to the namespace, hardcode ADMINS = (('admin', 'admin#example.com')) in local_settings,
from local_settings import ADMINS
As you do not want to expose the information to github, add local_settings.py to your .gitignore.
As #DJV said," ADMINS in settings.py and the superusers mean different things (the first are expected to manage the site, while the others to manage the content)", I do not think ADMINS is so important.
I wanted this code to run when the server is spun up not at each request and this solution seems to be doing just that. I don't want to hard code the ADMINS because i like the idea of using django's admin to manage users and since i am pulling the username and email from the database i don't have to worry about exposing the user's info info in the code.
I created a file called admins.py in my application and in init.py imported that file to execute the code. Now when a user is_superuser their info will be added to ADMINS automatically.
myapp.core.init.py
from myapp.core.admins import *
myapp.core.admins.py
from django.conf import settings
from django.contrib.auth.models import User
from django.core.mail import mail_admins
#admins setup: These users will get 500 error emails
admins = User.objects.filter(is_superuser__exact=True)
settings.ADMINS = []
for u in admins:
settings.ADMINS.append((u.username, u.email))
#send test email
if settings.SEND_TEST_EMAIL:
mail_admins('SEVER HAS BEEN STARTED', 'The server has been started # '+settings.SITE_IP, fail_silently=False)
UPDATE: There is an issue with the above solution. I had to rebuild my database and when i ran syncdb it fails due to a missing user model. I tried moving the import to models.py init but still no luck. Where is the first safe point of entry?? Do i have to create an admins model or create user profiles to extend auth/user and add an is_admin field? –
I have changed django.contrib.auth.user model where I have made the email id to be unique. After which I added a datamigration to reflect the same-
python manage.py datamigration appname unique_password.
Followed by python manage.py schemamigration appname --auto-->no changes. Finally, python manage.py migrate appname.
Migrating forwards to 0003_unique_password
appname:0037_unique_password.py
Now when I add a user whose email is not unique, it gives me an error and does not let me create the user through the django admin. But when i do:
>
python manage.py shell
from django.contrib.auth.models import User
user=User('cust1','123','xxx#gmail.com')
This creates a user object even though 'xxx#gmail.com' already exists.
How can I add an unique constraint to django.contrib.auth.user.email for an existing project (with data)?
Checks for uniqueness occur during model validation as per the Model validation documentation. Calling save() on the model doesn't automatically cause model validation. The admin application will be calling model validation.
Nothing prevents you from creating a User object in Python such as user=User('cust1','123','xxx#gmail.com') and then doing a user.save(). If there are uniqueness checks at the database level, you'll likely get an exception at this point. If you want to check before doing the save, call is_valid() on the Model instance.
If you want every User object to be automatically validated before save, use the pre_save Django signal to do Model validation before the save proceeds.
I'm getting this error message on my email field but i'm using the built in django auth system. Is there an easy way to override it. When the user registers the email address is added into the built in field within the built in user system.
Would be great if it's possible to make it extend it over 30 chars due to the nature of the site.
That is one of the issues with using email addresses for user names in Django. Many, many emails are over 30-characters. One common way to address this issues is using a custom "Authentication Backend" for email authentication. Using your own backend you can authenticate a user based on the email field instead of the username field. You can then generate the username based on that email address or using random generated usernames.
You can read more about this approach on my blog post Django Authentication using an Email Address.
Maybe it is not right way, but in my project i just increased user email size with south. Sample:
>> ./manage.py schemamigration auth --initial && ./manage migrate auth --fake
Then i added into models.py:
from django.contrib.auth.models import User
field = User._meta.get_field('email')
field.max_length = 254
field = User._meta.get_field('username')
field.max_length = 254
Now:
>> ./manage.py schemamigration auth --auto
>> ./manage.py migrate auth