5

के साथ एक सुपर उपयोगकर्ता बनाने के बाद django व्यवस्थापक में लॉगिन नहीं कर सकता मैं सफलतापूर्वक बनाए गए सुपरसुरर के साथ django व्यवस्थापक पैनल में लॉगिन करने के लिए घंटों तक प्रयास कर रहा हूं लेकिन सही उपयोगकर्ता नाम/पीडब्ल्यू कॉम्बो सही नहीं हो सकता है।कस्टम उपयोगकर्ता मॉडल

मैं चाहता हूं कि उपयोगकर्ता अपने उपयोगकर्ता नाम के रूप में अपने ईमेल का उपयोग करें। मैंने Django दस्तावेज़ here में उदाहरण को दोहराने के लिए भी अपना सर्वश्रेष्ठ प्रयास किया है। मैंने माइग्रेशन, sycndb हटा दिया है, और सब कुछ काम करता है सिवाय इसके कि व्यवस्थापक पैनल में लॉग इन को छोड़कर।

प्रासंगिक कोड: models.py से:

from django.db import models 
from django.forms import ModelForm 
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser 

class UserManager(BaseUserManager): 
    def create_user(self, email, password=None): 
     """ 
     Creates and saves a User with the given email 
     """ 
     if not email: 
      raise ValueError('Users must have an email address') 

     user = self.model(
      email=UserManager.normalize_email(email), 
     ) 
     user.set_password(password) 
     user.save(using=self._db) 
     return user 

    def create_superuser(self, email, password): 
     """ 
     Creates and saves a superuser with the given email, date of 
     birth and password. 
     """ 
     user = self.create_user(email, 
      password=password 
     ) 

     user.is_admin = True 
     user.is_staff = True 
     user.is_superuser = True 
     user.save(using=self._db) 
     return user 



class User(AbstractBaseUser): 
    objects = UserManager() 
    date_added = models.DateField(auto_now=False, auto_now_add=True) 
    email = models.EmailField(unique=True, db_index=True) 
    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS = [] 

    def __unicode__(self): 
     return self.email 

    is_active = models.BooleanField(default=True) 
    is_admin = models.BooleanField(default=False) 

    def get_full_name(self): 
    # The user is identified by their email address 
     return self.email 

    def get_short_name(self): 
    # The user is identified by their email address 
     return self.email 

    # On Python 3: def __str__(self): 
    def __unicode__(self): 
     return self.email 

    def has_perm(self, perm, obj=None): 

    # Simplest possible answer: Yes, always 
     return True 

    def has_module_perms(self, app_label): 

    # Simplest possible answer: Yes, always 
     return True 

    def is_staff(self): 

    # Simplest possible answer: All admins are staff 
     return self.is_admin 

से admin.py:

from django.contrib import admin 
from app.models import Relationship, Event, User 
from django import forms 
from django.contrib import admin 
from django.contrib.auth.models import Group 
from django.contrib.auth.admin import UserAdmin 
from django.contrib.auth.forms import ReadOnlyPasswordHashField 


class UserCreationForm(forms.ModelForm): 
    """A form for creating new users. Includes all the required 
    fields, plus a repeated password.""" 

    password1 = forms.CharField(label='Password', widget=forms.PasswordInput) 
    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) 

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

    def clean_password2(self): 
     # Check that the two password entries match 
     password1 = self.cleaned_data.get("password1") 
     password2 = self.cleaned_data.get("password2") 
     if password1 and password2 and password1 != password2: 
      raise forms.ValidationError("Passwords don't match") 
     return password2 

    def save(self, commit=True): 
     user = super(UserCreationForm, self).save(commit=False) 
     user.set_password(self.cleaned_data["password1"]) 
     if commit: 
      user.save() 
     return user 


class UserChangeForm(forms.ModelForm): 
    password = ReadOnlyPasswordHashField() 

    class Meta: 
     model = User 

    def clean_password(self): 
     return self.initial["password"] 





class UserAdmin(UserAdmin): 
    # The forms to add and change user instances 
    form = UserChangeForm 
    add_form = UserCreationForm 


    list_display = ('email', 'is_admin') 
    list_filter = ('is_admin',) 
    fieldsets = (
     (None, {'fields': ('email', 'password')}), 
     ('Permissions', {'fields': ('is_admin',)}), 
    ) 

    add_fieldsets = (
     (None, { 
      'classes': ('wide',), 
      'fields': ('email', 'password1', 'password2')} 
     ), 
    ) 
    search_fields = ('email',) 
    ordering = ('email',) 
    filter_horizontal =() 

admin.site.register(User, UserAdmin) 
admin.site.unregister(Group) 

प्रासंगिक settings.py कोड:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.contrib.auth.middleware.RemoteUserMiddleware', 
    # Uncomment the next line for simple clickjacking protection: 
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware', 
) 

ROOT_URLCONF = 'relrem.urls' 

# Python dotted path to the WSGI application used by Django's runserver. 
WSGI_APPLICATION = 'relrem.wsgi.application' 

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 

INSTALLED_APPS = (
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'django.contrib.admin', 
    'app', 
    'south', 

    # Uncomment the next line to enable admin documentation: 
    # 'django.contrib.admindocs', 
) 

AUTH_USER_MODEL = 'app.User' 

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.RemoteUserBackend', 
) 

एक सुपर उपयोगकर्ता बनाने और इसे देखने से नमूना टर्मिनल उत्पादनएक मेज:

Email: [email protected] 
Password: 
Password (again): 
Superuser created successfully. 

[ 
{ 
"pk": 1, 
"model": "app.user", 
"fields": { 
    "is_active": true, 
    "last_login": "2013-09-24T02:09:44.996Z", 
    "is_admin": true, 
    "date_added": "2013-09-23", 
    "password": "", 
    "email": "[email protected]" 
} 
} 
] 

मुझे लगता है कि जिस तरह से पासवर्ड सहेजा जा रहा है और वापस आ के साथ कुछ किया जाना चाहिए, क्योंकि कोई फर्क नहीं पड़ता कि मुझे क्या करना मैं "सही ईमेल और पासवर्ड एक कर्मचारी के खाते के लिए दर्ज करें । ध्यान दें कि दोनों फ़ील्ड केस-संवेदी हो सकते हैं। "संदेश। मेरा पासवर्ड मैंने सेट किया था" नमूना "। मैंने पीडब्ल्यू को हैश करने और इसे साफ करने से संबंधित सभी कोड को हटाने का प्रयास किया है, लेकिन वास्तव में यह अभी भी हैश में हैश उपयोगकर्ता तालिका।

मैं पहले से आशा है कि मैं कुछ स्पष्ट गलत कर रहा हूँ, धन्यवाद जो कोई समय इस पूरे प्रश्न के माध्यम से देखने के लिए लेता है।

उत्तर

9

कोड ठीक है। समस्या का उपयोग कर रहे है RemoteUserBackend विशेष रूप से, के बजाय default backend:

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.RemoteUserBackend', 
) 

मैं इसे अपने आप उपयोग नहीं किया है, लेकिन from the docs यह स्पष्ट है कि यह केवल आपके अनुरोधों में REMOTE_USER शीर्षलेख की जांच करेगा, इस प्रकार आपका पासवर्ड लॉगिन अप्रासंगिक प्रयास करता है।

यदि आप दोनों उपलब्ध करने के लिए wan't, एक वापस आने के रूप में डिफ़ॉल्ट ModelBackend जोड़ सकते हैं:

AUTHENTICATION_BACKENDS = (
     'django.contrib.auth.backends.RemoteUserBackend', 
     'django.contrib.auth.backends.ModelBackend', 
) 

या RemoteUserBackend की alltogether छुटकारा और अपने अनुप्रयोग डिफ़ॉल्ट तरीका प्रमाणित है मिलता है।

उम्मीद है कि इससे मदद मिलती है।

+0

इसे देखने के लिए समय निकालने के लिए धन्यवाद, दुर्भाग्यवश अभी भी एक ही समस्या है। मैंने AUTHENTICATION_BACKEND लाइन और RemoteUser से संबंधित कुछ भी हटा दिया है, और मैं अभी भी लॉगिन करने में असमर्थ हूं। – berserkia

+0

@berserkia क्या आपने अपने AUTHENTICATION_BACKENDS को 'django.contrib.auth.backends.ModelBackend' भी जोड़ा है? क्या आप अपना वर्तमान सेटिंग्स.py दिखाने के लिए अपना प्रश्न अपडेट कर सकते हैं? – kirbuchi

+0

यह मिला, धन्यवाद @kirbuchi। – berserkia

संबंधित मुद्दे