视图和表单的问题

时间:2012-08-30 09:14:45

标签: django django-forms django-views

我已将urls.py文件中的网址设为网址(r'^ register / $','drinker.views.drinker_reg'), 我在views.py文件中创建了drinker_reg视图。 但我仍然收到错误 ViewDoesNotExist at / register无法导入drinker.views.drinker_reg。模块drinker.views中不存在视图。

我注意到有一件事,当我使用drinker.forms导入RegistrationForm时,我收到错误,否则它会读取drinker_reg视图

views.py的代码是:

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from drinker.models import Drinker
from django.template import RequestContext
from drinker.forms import RegistrationForm

def drinker_reg(request):
    if request.user.is_authenticated():
         return HttpResponseRedirect("/profile/")
    if request.method == 'POST':
         pass
    else:
         #''' user is not submitting the form, show them a blank registration form '''

         #form = RegistrationForm()
         #context={'form':form}
         return render_to_response('registration.html',{'form':RegistrationForm()} , context_instance=RequestContext(request))

forms.py的代码是

from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from drinker.models import Drinker

class RegistrationForm(ModelForm):
       username   = forms.Charfield(label=(u'User Name'))
       email      = forms.EmailField(label=(u'Email Address'))
       password   = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
       password1   = forms.CharField(label=(u'Verify Password'), widget=forms.PasswordInput(render_value=False))

       class Meta:
              model=Drinker
              exclude=('user',)

       def clean_username(self):
                username=self.cleaned_data['username']
                try:
                      User.objects.get(username=username)
                except User.DoesNotExist:
                      return username
                raise forms.ValidationError("The Username is already taken, please try another.")
       def clean_password(self):
                password=self.cleaned_data['password']
                password1=self.cleaned_data['password1']
                if password != password1:
                    raise forms.ValidationError("The Password did not match, please try again.")
                return password

0 个答案:

没有答案