Harini
Harini

Reputation: 109

AttributeError: 'module' object has no attribute 'Name'

i am trying to create a basic form using python which has only one field your_name in the table NameForm. But i am getting the error AttributeError: 'module' object has no attribute 'Name'. I dont understand where this error comes from. Could anyone help me with it? I am using django 1.11. models.py

from __future__ import unicode_literals

from django.db import models

class NameForm(models.Model):
    your_name = models.CharField(max_length=200)

views.py

from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.views import generic

from .models import NameForm

class NameView(generic.NameView):
model = NameForm
template_name = 'home/name.html'

def get_name(request):
    if request.method == 'POST':
        form = NameForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/thanks/')
    else:
        form = NameForm()

    return render(request, 'name.html', {'form': form})

urls.py

from django.conf.urls import url

from . import views

app_name = 'home'

urlpatterns = [
url(r'^$', views.NameView.as_view(), name='name'),
]

template/home/name.html

<form action="/your-name/" method="post">
    <label for="your_name">Your name: </label>
    <input id="your_name" type="text" name="your_name" value="{{ current_name }}">
    <input type="submit" value="OK">
</form>

Upvotes: 0

Views: 4219

Answers (1)

Razia Khan
Razia Khan

Reputation: 478

You need to add generic.View Instead of generic.NameView, like this

from django.views import generic

class NameView(generic.View)
      # you code ...

Upvotes: 2

Related Questions