2016-09-20 75 views
1

我的網站已設置,因此沒有用戶名(或者user.username = user.email)。如果用戶嘗試輸入已經在數據庫中的用戶名,Django會出現錯誤消息,但由於我沒有使用用戶名進行註冊,因此我無法弄清楚如何執行此操作。Django 1.9檢查電子郵件已存在

就像現在的默認設置一樣,我不想重新加載頁面以查明是否有已經與用戶關聯的電子郵件地址。我的猜測是使用Ajax,但我無法弄清楚如何去做。我看了其他帖子,但似乎沒有任何近期的事情。

如何檢查電子郵件地址是否已經存在,如果有,請給出錯誤信息讓用戶輸入新的電子郵件地址?

models.py:

class MyUsers(models.Model): 
    user = models.OneToOneField(User) 
    first_name = models.CharField(max_length=100, blank=True) 
    last_name = models.CharField(max_length=100, blank=True) 
    email = models.EmailField(max_length=100, blank=True, unique=True) 
    company = models.CharField(max_length=100, blank=True, null=True) 
    website = models.URLField(max_length=100, blank=True, null=True) 
    phone_number = models.CharField(max_length=100, blank=True, null=True) 

    def __str__(self): 
     return self.user.username 

forms.py:

class UserForm(forms.ModelForm): 
    class Meta: 
     model = User 
     fields = ('email',) 


class UserProfileForm(forms.ModelForm): 
    class Meta: 
     model = UserProfile 
     fields = ('first_name', 'last_name', 'company', 'website', 'phone_number') 

views.py:

def index(request): 
    registered = False 

    if request.method == 'POST': 
     user_form = UserForm(data=request.POST) 
     profile_form = UserProfileForm(data=request.POST) 

     if user_form.is_valid() and profile_form.is_valid(): 
      user = user_form.save() 
      user.set_password(user.password) 
      user.password = "" 
      user.username = user.email 
      user.save() 

      profile = profile_form.save(commit=False) 
      profile.user = user 
      profile.email = user.email 
      profile.save() 

      user.first_name = profile.first_name 
      user.last_name = profile.last_name 
      user.save() 

      registered = True 
      return HttpResponseRedirect(reverse('registration')) 
     else: 
      print user_form.errors, profile_form.errors 
    else: 
     user_form = UserForm() 
     profile_form = UserProfileForm1() 

    context = {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} 
    return render(request, 'mysite/register.html', context) 

register.html:

{% extends 'mysite/base.html' %} 
{% load staticfiles %} 

{% block title_block %} 
    Register 
{% endblock %} 

{% block head_block %} 
{% endblock %} 

{% block body_block %}  
    <form id="user_form" method="post" action="/mysite/" enctype="multipart/form-data"> 
     {% csrf_token %} 
     {{ user_form.as_p }} 
     {{ profile_form.as_p }} 

     <input type="submit" name="submit" value="Register" /> 
    </form> 
{% endblock %} 
+0

這是一個非常寬泛的問題。這聽起來像你有代碼在某種程度上工作。確定下一步,盡最大努力解決問題,如果您有更具體的問題,我們可以幫助您。 – dckuehn

+0

我已經添加了我的文件以顯示我正在處理的代碼。下一步是弄清楚如果用戶已經輸入了相同的電子郵件地址,或者意外地使用了他人的電子郵件地址,該如何顯示用戶。 – Lefty

回答

1

您可以覆蓋UserForm上的clean_<INSERT_FIELD_HERE>()方法來檢查這種特定情況。它會是這個樣子:

forms.py:

class UserForm(forms.ModelForm): 
    class Meta: 
     model = User 
     fields = ('email',) 

    def clean_email(self): 
     # Get the email 
     email = self.cleaned_data.get('email') 

     # Check to see if any users already exist with this email as a username. 
     try: 
      match = User.objects.get(email=email) 
     except User.DoesNotExist: 
      # Unable to find a user, this is fine 
      return email 

     # A user was found with this as a username, raise an error. 
     raise forms.ValidationError('This email address is already in use.') 

class UserProfileForm(forms.ModelForm): 
    class Meta: 
     model = UserProfile 
     fields = ('first_name', 'last_name', 'company', 'website', 'phone_number') 

你可以閱讀更多有關清潔特定字段的形式在Django documentation約形式。

這就是說,我認爲你應該考慮創建一個custom user model而不是將你的User Profile類作爲User的包裝。

+1

我認爲這是'match = User.objects.get(email = email)'。 –

+0

@GeoffreyR。謝謝,我沒有注意到我有一個不同的論點。我已經更新了我的答案。 – tredzko