问题
I have given the url in my urls.py file as url(r'^register/$', 'drinker.views.drinker_reg'), and I have created the drinker_reg view in my views.py file. But still I am getting the error ViewDoesNotExist at /register Could not import drinker.views.drinker_reg. View does not exist in module drinker.views.
I notice one thing that when I used drinker.forms import RegistrationForm only then I am getting the error otherwise it read the drinker_reg view
The code of views.py is :
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))
the code of forms.py is
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
来源:https://stackoverflow.com/questions/12193569/issues-with-views-and-forms