Reputation: 35
These are the py files (error says unknown field(s) (model) specified for Post)
views.py
from django.db import models
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView
from .models import Post
from .forms import Post_form
# Create your views here.
#def home(request):
# return render(request, 'home.html', {})
class HomeView(ListView):
model = Post
template_name = 'home.html'
class ArticleDetailView(DetailView):
model = Post
template_name = 'article_details.html'
class AddPostView(CreateView):
model = Post
form_class = Post_form
template_name = 'add_post.html'
#fields = '__all__'
forms.py
from django import forms
from django.forms import widgets, ModelForm
from django import forms
from .models import Post
class Post_form(forms.ModelForm):
class Meta:
model = Post
fields = ('title','title_tag', 'model', 'body')
widgets = {
'title' : forms.TextInput(attrs={'class':'form-control'}),
'title_tag' : forms.TextInput(attrs={'class':'form-control'}),
'author' : forms.Select(attrs={'class':'form-control'}),
'title' : forms.Textarea(attrs={'class':'form-control'})
}
models.py
from typing import ChainMap
from django.db import models
from django.contrib.auth.models import User
from django.db.models.fields import CharField
from django.urls import reverse
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=255)
title_tag = models.CharField(max_length=255)
author = models.ForeignKey(User, on_delete=models.CASCADE) #Delete Post when User is deleted
body = models.TextField()
def __str__(self):
return self.title + ' | ' + str(self.author) #returns 'title | author' in admin page
def get_absolute_url(self):
return reverse("article_detail", args=(str(self.id)))
Error
File "/urls.py", line 3, in <module>
from .views import AddPostView, ArticleDetailView, HomeView
File "/views.py", line 5, in <module>
from .forms import Post_form
File "/forms.py", line 6, in <module>
class Post_form(forms.ModelForm):
File "/models.py", line 276, in __new__
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (model) specified for Post
I am new to django and still learning. Stuck here from quite long as the error message here is confusing. Any solutions? YouTube link which i am referring to - https://youtu.be/6-XXvUENY_8 Edit - models.py added
Upvotes: 2
Views: 5073
Reputation: 1
fields = ('title','title_tag', 'model', 'body')
you need to add the 'model' in field to your Model fields
soo
class Post(models.Model):
title =
title_tag =
model =
body =
Upvotes: 0
Reputation: 4973
Your class Post(models.Model)
doesn't have field with name model
From your model form
fields = ('title','title_tag', 'model', 'body')
Your Post model class should look like
class Post(models.Model):
title = ...
title_tag = ...
model = ... # HERE
body = ...
So either make sure the field exists or remove 'model' from fields tuple
Upvotes: 3