Hi there,
I am in the process of adding a custom user model to my app and upon doing so I am facing some difficulties. I’d like to add some fields for my CustomUser:
the relevant part in models.py:
class CustomGroup(Group):
# Define your custom fields here
class Meta:
proxy = True
class CustomUser(AbstractUser):
class Meta:
verbose_name = 'Custom User'
verbose_name_plural = 'Custom Users'
groups = models.ManyToManyField(
CustomGroup,
verbose_name='groups',
blank=True,
help_text='The groups this user belongs to.',
related_name='custom_user_groups' # Unique related name
)
user_permissions = models.ManyToManyField(
CustomPermission,
verbose_name='user permissions',
blank=True,
help_text='Specific permissions for this user.',
related_name='custom_user_permissions' # Unique related name
)
class UserProfile(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
learned_words = models.ManyToManyField('Word')
while I am contempt with using the regular auth.User for my superuser, for now.
I have the following in settings:
AUTH_USER_MODEL = "VocabTrainer.CustomUser"
if I change this to auth.User I can login using my superuser, which makes sense. If I have it like the above I cannot login with the created users, anyways …
urls.py
from django.conf import settings
from django.conf.urls.static import static
import django.contrib.auth.urls
urlpatterns = [
path('', IndexView.as_view(), name='index'),
path('word-list/', WordListView.as_view(), name='word-list-view'),
path('word-list/<str:language>/', LanguageWordListView.as_view(), name='lang-word-list-view'),
path('word-list/<str:language>/<int:pk>', WordFormView.as_view(), name='word-form'),
path('languages', LanguageList.as_view(), name='language-list'),
path('accounts/signup', SignUpView.as_view(), name='register'),
path('accounts/profile/<int:pk>/', UserProfileView.as_view(), name='profile'),
path('upload_csv', UploadCSVView.as_view(), name='upload-csv')
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
so, I want to have a form (templates/login.html) where I can either login with superuser or with regular user and it’s authenticated respectively.
The form is working, but only with AUTH_USER_MODEL = 'auth.User'
my form.py
{% extends "VocabTrainer/templates/base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login">
<input type="hidden" name="next" value="{{ next }}">
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endblock %}
can anyone help me with that?
all the best and thanks to everybody who reads this post and thinks about it!