Path('ajax/', views.validate_username, name = 'valid_usernmae'), ameError: name 'views' is not defined

views.py

from django.shortcuts import render
from django.views.generic import FormView
from .forms import JoinForm
from django.contrib.auth.forms import UserCreationForm
from django.views.generic.edit import CreateView
from django.http import JsonResponse

class SignUpView(CreateView):
template_name = ‘auth/signup.html’
form_class = UserCreationForm

def validate_username(request):
username = request.GET.get(‘username’, None)
data = {
‘is_taken’:User.objects.filter(username__iexact=username).exists()
}
return JsonResponse(data)

urls.py

from django.urls import path
from . views import SignUpView,validate_username

urlpatterns = [
path(‘signup/’, SignUpView.as_view(), name = ‘signup’),
# path(‘ajax/validate_username/’, views.validate_username, name = valid_usernmae),
path(‘ajax/’, views.validate_username, name = ‘valid_usernmae’),
]

and template

{% extends 'base.html' %}

{% block javascript %}

{% endblock javascript %}

{% block content %}

{% csrf_token %}
{{form.as_p}}
<button type="submit" >SignUp</button>

{% endblock content %}

Error I am getting
File “”, line 219, in _call_with_fram
File “C:\Users\GNB\Desktop\development\django\love\myapp\urls.py”,
module>
path(‘ajax/’, views.validate_username, name = ‘valid_usernmae’),
ameError: name ‘views’ is not defined

You imported the view as
from . views import SignUpView,validate_username

but you’re calling it this way:
path(‘ajax/’, views.validate_username, name = ‘valid_usernmae’),

change it to this:

path(‘ajax/’, validate_username, name = ‘valid_usernmae’),

1 Like

Thanks, brother ite works now.

1 Like