0

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>
6
  • 2
    But your views.py has no Name class... get_name is furthermore not a class-based view. Commented Jul 16, 2018 at 11:38
  • ``` class NameView(generic.NameView): model = NameForm template_name = 'home/name.html' ``` i have added this even then i am getting the same error Commented Jul 16, 2018 at 11:45
  • 1
    Then your URL should reference views.NameView, not views.Name. But there's no such thing as generic.NameView to inherit from anyway. Commented Jul 16, 2018 at 11:46
  • i have edited the code. I changed the url.. even then i have the same issue Commented Jul 16, 2018 at 11:52
  • No you don't. As I say, there is no such thing as generics.NameView, so the only thing this code would do is raise AttributeError. You are not actually running this code. Commented Jul 16, 2018 at 12:01

1 Answer 1

2

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

from django.views import generic

class NameView(generic.View)
      # you code ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.