CreateView and UpdateView based class are not working in Django Project - python

I have Django in Which there is app called Listing and it's model as follows but CreateView and UpdateView are not woking for model.
I am also using mixins but I have implemented Custom User , so is that cuases a problem?
getting error
in get_form return form_class(**self.get_form_kwargs())
TypeError: 'ListingForm' object is not callable
Any other user is updating listing of others, UserPassesTestMixin not working
import uuid
from django.db import models
from django.urls import reverse
from django.contrib.auth import get_user_model
User = get_user_model()
class Listing(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
...other fields
CreateView
class ListingCreateView(LoginRequiredMixin, CreateView):
model = Listing
form_class = ListingForm()
def form_valid(self, form):
form.instance.owner = self.request.user
return super().form_valid(form)
UpdateView
class ListingUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Listing
form_class = ListingForm()
def form_valid(self, form):
form.instance.owner = self.request.user
return super().form_valid(form)
def test_func(self):
listing = self.get_object()
#print(self.request.user, listing.owner)
if(self.request.user == listing.owner):
return True
else:
return False
forms.py
from django import forms
from .models import Listing
class ListingForm(forms.ModelForm):
class Meta:
model = Listing
fields = ['price', 'sqft', 'acre', 'title', 'description', 'address', 'city', 'state',
'country', 'zipcode', 'photo_main', 'photo_1', 'photo_2', 'photo_3', 'photo_4', 'photo_5']
Tysm for solving in advance!

You are missing form_class in your update and create view. You need to be add your forms name.
CreateView
class ListingCreateView(LoginRequiredMixin, CreateView):
model = Listing
form_class = your form name
fields = blah blah blah
def form_valid(self, form):
form.instance.owner = self.request.user
return super().form_valid(form)
UpdateView
class ListingUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Listing
form_class = your form name
def form_valid(self, form):
form.instance.owner = self.request.user
return super().form_valid(form)
def test_func(self):
listing = self.get_object()
#print(self.request.user, listing.owner)
if(self.request.user == listing.owner):
return True
else:
return False

Related

no such column: Blog_post.category_id

I’m trying to create a category for each post.
I made another class with the same models.py and in the same class Post I made a category = models.ForeignKey
But it keeps showing me this error when I run the server:
(no such column: Blog_post.category_id)
Ps: I did run the makemigrations and the migrate command.
The tutorial I followed just added the model as it is in models.py but should I also make a function for the views.py or its just a model problem ?
models.py
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Post(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, default="Some random thing")
werkstoffnummer = models.CharField(max_length=100)
def __str__(self):
return self.werkstoffnummer
def get_absolute_url(self):
return reverse("post-detail", kwargs={"pk": self.pk})
views.py
from random import choices
from unicodedata import category
from django import forms
from django.shortcuts import get_object_or_404, render, get_list_or_404, HttpResponse
from django.conf import settings
from django.http import HttpResponse, Http404
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.auth.models import User
from django.views.generic import (ListView, DetailView, CreateView, UpdateView, DeleteView)
from .models import Post, Category
from django.db.models import Q
import pandas as pd
import json
import os
def home(request):
context = {"posts": Post.objects.all()}
return render(request, "Blog/home.html", context)
def werkstoffdaten(request):
context = {"posts": Post.objects.all()}
return render(request, "Blog/werkstoffdaten.html", context)
class PostListView(ListView):
model = Post
template_name = "Blog/werkstoffdaten.html" # <app>/<model>_<viewtype>.html
context_object_name = "posts"
ordering = \["-date_posted"\]
paginate_by = 100
class PostListView(ListView):
model = Post
template_name = "Blog/home.html" # <app>/<model>_<viewtype>.html
context_object_name = "posts"
ordering = \["-date_posted"\]
paginate_by = 100
class UserPostListView(ListView):
model = Post
template_name = "Blog/user_posts.html" # <app>/<model>_<viewtype>.html
context_object_name = "posts"
paginate_by = 100
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get("username"))
return Post.objects.filter(author=user).order_by("-date_posted")
class PostDetailView(DetailView):
model = Post
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = \[
"werkstoffnummer", "werkstoffbezeichnung",
\]
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Post
fields = \[
"werkstoffnummer", "werkstoffbezeichnung",
\]
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
model = Post
success_url = "/"
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
def Periodic_Table(request):
return render(request, "Blog/Periodic_Table.html")
def user_manual(request):
return render(request, "Blog/user_manual.html")
def about(request):
return render(request, "Blog/about.html", {"title": "About"})
def search_post(request):
if request.method == "POST":
searched = request.POST.get("searched")
posts = Post.objects.filter(
Q(werkstoffnummer=searched) | Q(werkstoffbezeichnung=searched) | Q(gruppe=searched)
)
return render(
request, "Blog/search_post.html", {"searched": searched, "posts": posts}
)
else:
return render(request, "Blog/search_post.html", {})
def download(request, path):
file_path = os.path.join(settings.MEDIA_ROOT, path)
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
response\['Content-Disposition'\] = 'inline; filename=' + os.path.basename(file_path)
return response
raise Http404
OperationalError at /
https://imgur.com/a/lVDedeq
Delete default="Some random thing" in your category field, default value for foreign key of your Category model, must be a private key (pk (aka id (type == integer))), but not the string.
If you just delete this default value, it will work, or you can put default=1 as first object of your Category models would be default for your new Post objects
class Post(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
or
class Post(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, default=1)

How can I convert my class-based view to function-based views? - Django

I am trying to change all my class-based views to function-based views and I am having difficulty converting the following class:
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, SuccessMessageMixin, UpdateView):
model = Post
fields = ['image', 'title', 'category', status', 'description']
template_name = 'blog/post-form.html'
success_message = 'Your post has been updated.'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
def get_context_data(self, **kwargs):
context = super(PostUpdateView, self).get_context_data(**kwargs)
context['title'] = 'Update post'
context['submit'] = 'Update'
return context
def get_success_url(self):
author = self.object.author
return reverse_lazy( 'profile', kwargs={'author': author.username})
The function and form should do exactly what this class-based view does, so if anyone can help me out, please let me know.
You can specify a .form_class attribute [Django-doc] in an UpdateView (as well as in a CreateView). So we can create a form like:
# app/forms.py
from django import forms
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['image', 'title', 'category', 'status', 'description']
widgets = {
'image': …
}
Where you replace … with the widget you want to use for the image field.
Then you can plug in that form:
# app/views.py
from app.forms import PostForm
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, SuccessMessageMixin, UpdateView):
model = Post
form_class = PostForm
template_name = 'blog/post-form.html'
success_message = 'Your post has been updated.'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
def get_context_data(self, **kwargs):
context = super(PostUpdateView, self).get_context_data(**kwargs)
context['title'] = 'Update post'
context['submit'] = 'Update'
return context
def get_success_url(self):
author = self.object.author
return reverse_lazy( 'profile', kwargs={'author': author.username})
Behind the curtains, if you do not specify a form_class, Django will simply make one for you with the modelform_factory [Django-doc], so by using another ModelForm, we do not change the logic of using the form.

success_url from UpdateView to DetailView with pk in django

I make my own group permission page self with ListView to show Django Group list with a link to DetailView with pk and then give the change link to change the permissions with UpdateView.
I want to make the success_url of UpdateView to DetailView with pk, how to do this.
my code like:
views.py
class GroupCreation(PermissionRequiredMixin, CreateView):
permission_required = 'add_group'
model = Group
form_class = GroupCreateModelForm
success_url = reverse_lazy('account:group_list')
template_name = 'accounts/groups/group_creation.html'
class GroupsListView(PermissionRequiredMixin, ListView):
permission_required = 'view_group'
allow_empty = True
model = Group
ordering = 'name'
template_name = 'accounts/groups/groups_list.html'
form = GroupCreateModelForm
extra_context = {'form': form, }
class GroupDetailView(PermissionRequiredMixin, DetailView):
permission_required = 'view_group'
model = Group
template_name = 'accounts/groups/group_detail.html'
class GroupUpdateView(PermissionRequiredMixin, UpdateView):
permission_required = 'change_group'
model = Group
fields = ('permissions',)
template_name = 'accounts/groups/group_update.html'
success_url = reverse_lazy('account:group_detail')
urls.py
path('groups/', views.GroupsListView.as_view(), name='group_list'),
path('groups/<int:pk>/', views.GroupDetailView.as_view(), name='group_detail'),
path('groups/<int:pk>/change/', views.GroupUpdateView.as_view(), name='group_change'),
path('groups/create/', views.GroupCreation.as_view(), name='group_creation'),
You can use the get_success_url method in your UpdateView instead of success_url attribute; This way you can access the edited object's pk:
def get_success_url(self):
return reverse_lazy('account:group_detail', kwargs={'pk': self.object.pk})

Problem using unique_together and saving authenticated user

There is a problem in my code for two fields entry uniqueness checking.
I defined a model with unique_together to check uniqueness of a field records for each user, but it accepts duplicated entry added by that user.
model.py
from django.db import models
from django.contrib.auth.models import User
class UserItem(models.Model):
definer = models.ForeignKey(User, on_delete=models.CASCADE)
item_name = models.CharField(max_length=50)
.
.
class Meta:
unique_together = ("definer", "item_name")
views.py
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic.edit import CreateView, UpdateView, DeleteView
class RecordCreateView(LoginRequiredMixin, CreateView):
model = UserItem
template_name = 'item_new.html'
#excluding "definer" field and inserting its value by form_valid
fields = ['item_name', . . .]
def form_valid(self, form):
form.instance.definer = self.request.user
return super().form_valid(form)
I expect warning and preventing users to add new record with the same "item_name" added before by themselves, but it accepts them (without warning).
When I replace "definer" with other fields, it works fine and warns for duplicate records. Additionally when records added by admin, it works and there will be the expected warning.
I guess, this problem is for that the authenticated user is inserted as "definer" by "def form_valid" after the "unique_together = ("definer", "item_name")" has done its role. On the other hand, uniqueness checking in done when the "definer" is empty.
What should I do to solve this problem?
Edit: Adding full model
```` Full Model in model.py
class UserItem(models.Model):
item_type = models.CharField(max_length=12, verbose_name='Item type')
item_name = models.CharField(max_length=50)
bound = models.CharField(null=True, blank=True, default=None, max_length=4, verbose_name='Bound')
price = models.FloatField(default=0)
maximum_use = models.FloatField(default=0, verbose_name='Maximum use (%)’)
matterial = models.FloatField(null=True, blank=True, default=None, verbose_name='matterial (%)')
energy = models.FloatField(null=True, blank=True, default=None, verbose_name='energy (kcal/k)')
definer = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return "{}, name: {}, definer: {}".format(self.item_type, self.item_name, self.definer,)
def get_absolute_url(self):
return reverse('profile')
class Meta:
unique_together = ("definer", "item_name")
````
```` views.py after #Pedro suggestion to edit
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.contrib.auth.models import User
from django.contrib.auth.mixins import LoginRequiredMixin
class RecordCreateView(LoginRequiredMixin, CreateView):
model = UserItem
template_name = 'item_new.html'
fields = ['item_name', 'matterial', 'energy',]
def get_success_url(self):
return reverse('profile')
def form_valid(self, form):
user_item = form.save(commit=False)
user_item.definer = self.request.user
user_item.item_type = 'required'
user_item.bound = 'min'
try:
user_item.save()
except IntegrityError:
form.add_error('item_name', 'Item name is repeated')
return self.form_invalid(form)
return HttpResponseRedirect(self.get_success_url())
````
Thanks to #Pedro's very helpful hints; Lastly I could solve my problem by some changes in his code.
Also I removed this part in model.py:
"class meta:
unique_together = ("definer", "item_name")"
````views.py
class RecordCreateView(LoginRequiredMixin, CreateView):
model = UserItem
template_name = 'item_new.html'
fields = ['item_name', 'matterial', 'energy',]
def form_valid(self, form):
user_items = form.save(commit=False)
item_name = user_items.item_name
qs = UserItemComposition.objects.filter(definer=self.request.user, item_name=item_name)
if qs.exists():
form.add_error('item_name', 'Item name is repeated')
return self.form_invalid(form)
form.instance.definer = self.request.user
return super().form_valid(form)
````
The problem is you are adding the definer after the form is validated. You can pass the request.user as the initial data, like this:
class RecordCreateView(LoginRequiredMixin, CreateView):
model = UserItem
template_name = 'item_new.html'
#excluding "definer" field and inserting its value by form_valid
fields = ['item_name', 'definer', ...]
def get_initial(self):
initial = super().get_initial()
initial['definer'] = self.request.user
return initial
Now you don't need to override form_valid.
Edit: If you don't want the definer in the form fields you can do this:
class RecordCreateView(LoginRequiredMixin, CreateView):
model = UserItem
template_name = 'item_new.html'
fields = ['item_name', ...]
def form_valid(self, form):
user_item = form.save(commit=False)
user_item.definer = self.request.user
try:
user_item.save() # should raise an exception if unique_together constrain fails
except ValidationError:
form.add_error('item_name', 'Item name is repeated') # add custom error to form
return self.form_invalid(form) # return the invalid form
return HttpResponseRedirect(self.get_success_url())

Empty django models

Hi I'm getting tottaly empty Comments. And I don't really know why.
Here is my view file.
from django.shortcuts import render, redirect
from .forms import PostForm
from django.views.generic import TemplateView
from .forms import CommentForm
from django.shortcuts import get_object_or_404
from .models import Post
class createPost(TemplateView):
template_name = 'forum/createPost.html'
def get(self, request):
form = PostForm()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = PostForm(request.POST)
if(form.is_valid()):
form.save()
return redirect('/forum')
def add_comment(request, pk):
post = get_object_or_404(Post, pk=pk)
if(request.method == 'POST'):
form = CommentForm(request.POST)
if(form.is_valid()):
comment = form.save(commit=False)
comment.post = post
comment.save()
return redirect('/forum/')
else:
form = CommentForm()
template = 'forum/addComment.html'
context = {'form': form}
return render(request, template, context)
And here is my models file
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=140)
body = models.CharField(max_length=500)
date = models.DateTimeField()
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comments', null=True, on_delete=models.SET_NULL)
com_title = models.CharField(max_length=140)
com_body = models.CharField(max_length=500)
def __str__(self):
return self.com_title
And lastly here is forms
from django import forms
from .models import Post, Comment
class PostForm(forms.ModelForm):
title = forms.CharField(max_length=140)
body = forms.CharField()
date = forms.DateTimeField()
class Meta:
model = Post
fields = ('title', 'body', 'date')
class CommentForm(forms.ModelForm):
title = forms.CharField(max_length=140)
body = forms.CharField(max_length=500)
class Meta:
model = Comment
fields = ('title', 'body')
I don't really know why I'm getting this error. I get a comment but It is totaly blank. Mabye It has something to do with the comment = form.save(commit=False), but i don't know.
I am really new to Django so please let me know if you know how to solve it. Also if there is somthing more I have to add to this question like urls and stuff please let me know.
Thanks ;)
Try changing your view to
class createPost(CreateView):
template_name = 'forum/createPost.html'
model=Post
form_class=PostForm
def form_valid(self, form):
form.save()
return http.HttpResponseRedirect('/forum')
and then form to
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'body', 'date')
PS: If you don't want to make any changes to your form fields, then form_class is not required. You may provide the fields to the View itself.

Categories