我正在尝试使用python创建一种基本形式,该形式在表your_name
中只有一个字段NameForm
。但是我收到错误AttributeError: 'module' object has no attribute 'Name'
。我不知道此错误来自何处。有人可以帮我吗?我正在使用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>
答案 0 :(得分:1)
您需要像这样添加generic.View
而不是generic.NameView
from django.views import generic
class NameView(generic.View)
# you code ...