Monday, January 18, 2016

ModelForm has no model class specified

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django import forms
from django.forms import ModelForm

# Create your models here.

class Artist(models.Model):
#code
name = models.CharField("artist", max_length=50)
year_formed = models.PositiveIntegerField()

def __str__(self):
return self.name


class ArtistForm(forms.ModelForm):
class meta:
model = Artist
fields = ['name', 'year_formed']


class Album(models.Model):
name = models.CharField("album", max_length=50)
artist = models.ForeignKey(Artist)

def __str__(self):
return self.name


from django.shortcuts import render, render_to_response
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.template import RequestContext
from Album.models import *
#from Album.forms import *

# Create your views here.
def artistform(request):
if request.method == "GET":
form = ArtistForm()
return render('Album/form.html', {'form': form})
elif request.method == "POST":
form = ArtistForm(request.POST)
form.save()
return HttpResponseRedirect('/artists')

def artists(request):
artists = Artist.objects.all()
return render_to_response('Album/artists.html', {'artists': artists})

def artistdetails(request, id):
artist = Artist.objects.get(pk = id)
return render_to_response('Album/artistdetails.html', {'artists': artists})

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/artists/form

Django Version: 1.8.7
Python Version: 2.7.11
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'Album')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/corecode/Desktop/Python course/ms-django/music_album/Album/views.py" in artistform
  10.         form = ArtistForm()
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in __init__
  313.             raise ValueError('ModelForm has no model class specified.')

Exception Type: ValueError at /artists/form
Exception Value: ModelForm has no model class specified.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bf32dca1-31e2-4f66-91f0-ed0ac5729636%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment