在django中创建注册模板时出错。怎么调试?

时间:2011-06-11 14:33:14

标签: django

这是我的forms.py

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.)

使用这些模板是个好主意还是应该使用我自己的自定义模板?

2 个答案:

答案 0 :(得分:1)

哇,您使用的是什么版本的Django? forms.Manipulator在1.0版本中被删除 - 三年前 - 在此之前被弃用了一年。

答案 1 :(得分:0)

我猜你的项目有一个导入问题 - 但是Django抓住了这个并没有向你展示真正的错误。请注意,这不需要从settings.py导入 - 它可以在项目的任何位置导入。

尝试跑步:

python settings.py

python应该告诉你导入问题是什么。如果没有输出,你的问题就在其他地方 - 但这很可能是候选人。