hi
thank you for such an awsome tutorial, but i got stuck while creating signup, i been following ur video, you got the same error and u fixed it with User = get**model();
but even after following you, i still get that error,
##########################################################################################
leads/forms.py
from django import forms
from .models import Lead
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UsernameField
User = get_user_model()
class LeadModelForm(forms.ModelForm):
class Meta:
model = Lead
fields =(
'first_name',
'last_name',
'age',
'agent'
)
class LeadForm(forms.Form):
first_name = forms.CharField()
last_name = forms.CharField()
age = forms.IntegerField(min_value=0)
class CustomUserCreationForm(UserCreationForm):
class meta:
model = User
fields = {"username",}
field_classes = {'username': UsernameField}
###############################################################################
djcrm/urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.auth.views import LoginView, LogoutView
from django.urls import path, include
from leads.views import landing_page, LandingPageView, SignupView
urlpatterns = [
path('admin/', admin.site.urls),
# path('', landing_page, name='landing-page'),
path('', LandingPageView.as_view(), name='landing-page'),
path('leads/', include('leads.urls', namespace="leads")),
path('login/', LoginView.as_view(), name='login'),
path('signup/', SignupView.as_view(), name='signup'),
path('logout/', LogoutView.as_view(), name='logout')
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
##############################################################################
leads/views.py
from django.shortcuts import render, redirect, reverse
#from django.contrib.auth.forms import UserCreationForm
from django.core.mail import send_mail
from django.http import HttpResponse
from .models import Lead, Agent
from .forms import LeadForm, LeadModelForm,CustomUserCreationForm
from django.views import generic
CRUD+L create retrieve update delete + list
class SignupView(generic.CreateView):
template_name = "registration/signup.html"
form_class = CustomUserCreationForm
def get_success_url(self):
return reverse("login")
class LandingPageView(generic.TemplateView):
template_name = "landing.html"
def landing_page(request):
return render(request, "landing.html")
class LeadListView(generic.ListView):
template_name = "leads/lead_list.html"
queryset = Lead.objects.all()
context_object_name = "leads"
def lead_list(request):
#return HttpResponse("hello_world")
leads = Lead.objects.all()
context = {
"leads": leads
}
return render(request, "leads/lead_list.html", context)
class LeadDetailView(generic.DetailView):
template_name = "leads/lead_detail.html"
queryset = Lead.objects.all()
context_object_name = "lead"
def lead_detail(request, pk):
lead = Lead.objects.get(id=pk)
context = {
"lead": lead
}
return render(request, "leads/lead_detail.html", context)
class LeadCreateView(generic.CreateView):
template_name = "leads/lead_create.html"
form_class = LeadModelForm
def get_success_url(self):
return reverse("leads:lead-list")
def form_valid(self, form):
#trigger email
send_mail(
subject="hello new lead entry",
message="please check admin console, new lead entry",
from_email="[email protected]",
recipient_list=["[email protected]","[email protected]"]
)
return super(LeadCreateView, self).form_valid(form)
def lead_create(request):
form = LeadModelForm()
if request.method == "POST":
print('Receiving a post requet')
form =LeadModelForm(request.POST)
if form.is_valid():
form.save()
return redirect("/leads")
context = {
"form": form
}
return render(request, "leads/lead_create.html", context)
class LeadUpdateView(generic.UpdateView):
template_name = "leads/lead_update.html"
queryset = Lead.objects.all()
form_class = LeadModelForm
def get_success_url(self):
return reverse("leads:lead-list")
def lead_update(request, pk):
lead = Lead.objects.get(id=pk)
form = LeadModelForm(instance=lead)
if request.method == "POST":
form =LeadModelForm(request.POST, instance=lead)
if form.is_valid():
form.save()
return redirect("/leads")
context = {
"form": form,
"lead": lead
}
return render(request, "leads/lead_update.html", context)
def lead_delete(request, pk):
lead = Lead.objects.get(id=pk)
lead.delete()
return redirect("/leads")
class LeadDeleteView(generic.DeleteView):
template_name = "leads/lead_delete.html"
queryset = Lead.objects.all()
def get_success_url(self):
return reverse("leads:lead-list")
############################################################################################
traceback
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/signup/
Django Version: 3.1.4
Python Version: 3.9.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'leads']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\views\generic\edit.py", line 172, in post
return super().post(request, *args, **kwargs)
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\views\generic\edit.py", line 141, in post
if form.is_valid():
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\forms\forms.py", line 177, in is_valid
return self.is_bound and not self.errors
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\forms\forms.py", line 172, in errors
self.full_clean()
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\forms\forms.py", line 376, in full_clean
self._post_clean()
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\contrib\auth\forms.py", line 124, in _post_clean
super()._post_clean()
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\forms\models.py", line 405, in _post_clean
self.instance.full_clean(exclude=exclude, validate_unique=False)
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\db\models\base.py", line 1216, in full_clean
self.clean()
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\contrib\auth\models.py", line 365, in clean
self.email = self.class.objects.normalize_email(self.email)
File "C:\Users\repairmen\Desktop\crm\getting-started-with-django\env\lib\site-packages\django\db\models\manager.py", line 187, in get
raise AttributeError(
Exception Type: AttributeError at /signup/
Exception Value: Manager isn't available; 'auth.User' has been swapped for 'leads.User'