model form does not work

37 views
Skip to first unread message

prashant joshi

unread,
Oct 10, 2016, 11:12:46 AM10/10/16
to Django users
i created model form




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



CATEGORIES = ( 
    ('mh', 'Maharashtra'),
    ('pb', 'Punjab'),
    ('gj', 'Gujarat'),
   
)
Country = ( 
    ('ind', 'India'),
    ('uk', 'United Kingdom'),
    ('sa', 'South Africa'),
   
)


class holidaytime(models.Model):
    MYdob= models.DateField()
    Time= models.DateTimeField()
def __str__(self):
     return self.name
class Registerform(models.Model):
    FirstName = models.CharField(max_length=200)
    LastName = models.CharField(max_length=200)
    Myprofile = models.FileField(upload_to='uploads/')
    Myresume = models.FileField(upload_to='uploads/')
    Bio = models.TextField()
    State= models.CharField(max_length=3, choices=CATEGORIES)
    Country= models.CharField(max_length=3, choices=Country)


def __str__(self):








forms,py

from django import forms
from .models import Registerform
from django.forms import ModelForm

class Regform(forms.ModelForm):
    class meta:
        model=Registerform
        fields=["FirstName","LastName","Myprofile","Myresume","Bio","State","Country"]
     return self.name



views.py


from django.shortcuts import render
from .forms import Regform
# Create your views here.
def register(request):
    s=Regform(request.POST)
    return render(request, 'webpage/register.html',{"s":s})



register.html
<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
</head>
<body>
<h1>Registration Form</h1>
<form>{% csrf_token %}
    {{form.as_p}}
</form>

</body>
</html>




it is not worked..it show

ValueError at /task/

ModelForm has no model class specified. this error





ludovic coues

unread,
Oct 10, 2016, 11:27:24 AM10/10/16
to django...@googlegroups.com
Most of the time, it help to provide the full stack trace that come
with the error. The one in the console where you started django should
be easier to copy and paste.

In your case, it look like a typo. In forms.py, it should be `class
Meta` and you wrote `class meta`.


An alternative is to use class based views. I wrote a few examples
covering the most useful case on stackoverflow [1]. For your code, it
boild down to replace the view with that:

from django.core.urlresolvers import reverse_lazy
from django.views.generic.edit import CreateView
from .models import Registerform

class RegisterView(CreateView):
template_name = "register.html"
model = Registerform
fields = ["FirstName", "LastName", "Myprofile", "Myresume",
"Bio", "State", "Country"]
success_url = reverse_lazy("app:views")

Then in your urls.py file, you use RegisterView.as_view() instead of
register. The RegisterView will take care of creating a form based on
the models, validating the data, saving the object in database,
handling get and post request.


[1] http://stackoverflow.com/documentation/django/1220/class-based-views
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/6d5714b1-c526-4851-a6c9-4700498fc579%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Andrea D'Amore

unread,
Oct 12, 2016, 6:43:40 AM10/12/16
to django...@googlegroups.com
It's hard to say if that's actual code copied and pasted, but maybe
it's this part:

> class Regform(forms.ModelForm):
> class meta:

> ModelForm has no model class specified. this error

Personal suggestions: paste a minimal working example, when you do su
use text only format messages to keep code properly formatted, use
conventional python spacing for ease of read, use proper naming for
classes -i.e. don't call your model "RegisterForm" and then your form
"Regform".


--
Andrea
Reply all
Reply to author
Forward
0 new messages