2011-06-11 101 views
0

這是我forms.py在django中創建註冊模板時出錯。如何調試?

from django import forms 
from django.core import validators 
from django.contrib.auth.models import User 

class RegistrationForm(forms.Manipulator): 
    def __init__(self): 
     self.fields = (
      forms.TextField(field_name='username', 
          length=30, maxlength=30, 
          is_required=True, validator_list=[validators.isAlphaNumeric, 
                   self.isValidUsername]), 
      forms.EmailField(field_name='email', 
          length=30, 
          maxlength=30, 
          is_required=True), 
      forms.PasswordField(field_name='password1', 
           length=30, 
           maxlength=60, 
           is_required=True), 
      forms.PasswordField(field_name='password2', 
           length=30, maxlength=60, 
           is_required=True, 
           validator_list=[validators.AlwaysMatchesOtherField('password1', 
                        'Passwords must match.')]), 
      ) 

    def isValidUsername(self, field_data, all_data): 
     try: 
      User.objects.get(username=field_data) 
     except User.DoesNotExist: 
      return 
     raise validators.ValidationError('The username "%s" is already taken.' % field_data) 

    def save(self, new_data): 
     u = User.objects.create_user(new_data['username'], 
            new_data['email'], 
            new_data['password1']) 
     u.is_active = False 
     u.save() 
     return u 

這是我的views.py

from django.contrib.auth import authenticate, login 
from django.shortcuts import render_to_response 

import datetime, random, sha 
from django.shortcuts import render_to_response, get_object_or_404 
from django.core.mail import send_mail 





def login(request): 
    def errorHandle(error): 
     form = LoginForm() 
     return render_to_response('login.html', { 
       'error' : error, 
       'form' : form, 
     }) 
    if request.method == 'POST': # If the form has been submitted... 
     form = LoginForm(request.POST) # A form bound to the POST data 
     if form.is_valid(): # All validation rules pass 
      username = request.POST['username'] 
      password = request.POST['password'] 
      user = authenticate(username=username, password=password) 
      if user is not None: 
       if user.is_active: 
        # Redirect to a success page. 
        login(request, user) 
        return render_to_response('userprof/why.html', { 
         'username': username, 
        }) 
       else: 
        # Return a 'disabled account' error message 
        error = u'account disabled' 
        return errorHandle(error) 
      else: 
       # Return an 'invalid login' error message. 
       error = u'invalid login' 
       return errorHandle(error) 
     else: 
      error = u'form is invalid' 
      return errorHandle(error) 
    else: 
     form = LoginForm() # An unbound form 
     return render_to_response('login.html', { 
      'form': form, 
     }) 



def loggedin(request): 

    return render_to_response('loggedin.html', {}) 


def register(request): 
    if request.user.is_authenticated(): 
     # They already have an account; don't let them register again 
     return render_to_response('userprof/register.html', {'has_account': True}) 
    manipulator = RegistrationForm() 
    if request.POST: 
     new_data = request.POST.copy() 
     errors = manipulator.get_validation_errors(new_data) 
     if not errors: 
      # Save the user                                     
      manipulator.do_html2python(new_data) 
      new_user = manipulator.save(new_data) 

      # Build the activation key for their account                              
      salt = sha.new(str(random.random())).hexdigest()[:5] 
      activation_key = sha.new(salt+new_user.username).hexdigest() 
      key_expires = datetime.datetime.today() + datetime.timedelta(2) 

      # Create and save their profile                                 
      new_profile = UserProfile(user=new_user, 
             activation_key=activation_key, 
             key_expires=key_expires) 
      new_profile.save() 

      # Send an email with the confirmation link                              
      email_subject = 'Your new example.com account confirmation' 


      return render_to_response('userprof/register.html', {'created': True}) 
    else: 
     errors = new_data = {} 
    form = forms.FormWrapper(manipulator, new_data, errors) 
    return render_to_response('userprof/register.html', {'form': form}) 


def confirm(request, activation_key): 
    if request.user.is_authenticated(): 
     return render_to_response('userprof/confirm.html', {'has_account': True}) 
    user_profile = get_object_or_404(UserProfile, 
            activation_key=activation_key) 
    if user_profile.key_expires < datetime.datetime.today(): 
     return render_to_response('confirm.html', {'expired': True}) 
    user_account = user_profile.user 
    user_account.is_active = True 
    user_account.save() 
    return render_to_response('confirm.html', {'success': True}) 

這是我計劃使用 https://github.com/yourcelf/django-registration-defaults/tree/master/registration_defaults/templates的模板。

根據它,我的確在settings.py的變化,但它給了我一個錯誤

Error: Can't find the file 'settings.py' in the directory containing 'manage.py'. It appears you've customized things. 
You'll have to run django-admin.py, passing it your settings module. 
(If the file settings.py does indeed exist, it's causing an ImportError somehow.) 

是否使用這些模板或者我應該去爲自己的自定義模板是一個好主意?

回答

0

我猜你的項目有一個導入問題 - 但Django抓住了這一點,並沒有告訴你真正的錯誤。請注意,這不需要從settings.py導入 - 它可以是項目中任何位置的導入。

嘗試運行:

python settings.py 

和Python應該告訴你的進口問題是什麼。如果沒有產出,你的問題就在別處 - 但這是一個很有可能的候選人。

+0

我還有一個簡單的問題。如何編寫處理註冊的django模板。給出一個我可以輸入用戶名,密碼和電子郵件的模板。就這樣 。這對於我的項目來說太過於樂觀了 – Hick 2011-06-11 14:53:09

1

哇,你使用的是什麼版本的Django? forms.Manipulator在三年前在版本1.0中被刪除 - 並且在此之前已被棄用一年。