xMrAli
February 23, 2023, 4:06pm
21
now it’s shows 500
UnicodeEncodeError: ‘ascii’ codec can’t encode character ‘\u062a’ in position 0: ordinal not in range(128)
class BlogDetailView(DetailView):
model = Blog
template_name = 'Blogs/BlogDetails.html'
def get_object(self, **kwargs):
context = {}
instance = Blog.objects.filter(slug=unquote(self.kwargs.get('slug')))[:1].get()
print(context)
print("Slug", self.kwargs.get('slug'))
print("After quote: ", unquote(self.kwargs.get('slug')))
year = self.kwargs.get('year')
reports = Report.objects.filter(Blog=instance)
years = Year.objects.filter(name__gte=str(instance.since), report__in=reports)
context['instance'] = instance
context['years'] = years
context['year'] = year
# print("Year: ", year)
# print("Years:", years)
return context
def post(self, request, *args, **kwargs):
year = self.request.POST.get('year_choice', None)
instance = self.get_object()
if not year:
return redirect('Blogs:Blog_detail', slug=instance.slug)
return redirect('reports:Blog_report', slug=instance.slug, year=year)
Side note: Only the process to get your instance
should be in get_object
. It should also be returned as the properly named values on self
, that is self.object
and self.blog
in this case. Everything else still belongs in get_context_data
.
Review the docs at Single object mixins | Django documentation | Django along with the detailed information about DetailView at DetailView -- Classy CBV . You want to become more familiar with the flow of events through the CBVs.
As with every error posted here, please post the complete traceback.
xMrAli
February 28, 2023, 5:45pm
23
Now BlogDetailView
works (but function based view ) And i am happy its work.
but BlogUpdateView
still shows 404
can anyone help me with BlogUpdateView
class or function, i am really stuck
import django_tables2 as tables
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.db.models import Q
from django.shortcuts import redirect, get_object_or_404, render
from django.views.generic import DetailView, UpdateView, CreateView, TemplateView
from django_tables2 import RequestConfig
from blogs.forms import BloganizationForm
from blogs.models import Blog
from blogs.tables import DonationTable, BlogReportTable, BlogListTable
from reports.models import Donation, Report
from years.models import Year
from urllib.parse import unquote
from django.db.models import Sum, F, Max
class BlogDetailView(tables.SingleTableView):
model = Donation
table_class = DonationTable
template_name = 'blogs/blogDetails.html'
def get_queryset(self):
blog = get_object_or_404(Blog, slug=self.kwargs.get('slug'))
queryset = Report.objects.filter(blog=blog).values('year__name','blog__slug').annotate(asset=Sum('asset'), donation=Sum('donation'), expense=Sum('expense'), highest=Sum('id'))
return queryset
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
slug = unquote(self.kwargs.get('slug'))
print(slug)
context['object'] = get_object_or_404(Blog, slug=slug)
instance = context['object']
year = self.kwargs.get('year')
reports = Report.objects.filter(blog=instance)
years = Year.objects.filter(name__gte=str(instance.since), report__in=reports)
context['years'] = years
context['year'] = year
return context
class BlogUpdateView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
model = Blog
template_name = 'blogs/blogForm.html'
form_class = BloganizationForm
success_message = "good."
def get_object(self, queryset=None):
slug = self.kwargs.get('slug')
return get_object_or_404(Blog, slug=slug, user=self.request.user)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['request'] = self.request
return kwargs
What makes you think that the solution for your update view is fundamentally any different than your previous solution? (Hint: It’s not. You need to address the same issues here as you handled previously.)
xMrAli
February 28, 2023, 5:58pm
25
you mean?
from urllib.parse import unquote
for me didn’t work in classed based view
i don’t know why, i may need to fine a good code mentor.
That’s just importing the library.
You still need to use that library to translate the submitted slug, in the same way you unqote
the arg in your other view.
xMrAli
February 28, 2023, 6:19pm
27
i did try
def get_object(self, queryset=None):
slug = unquote(self.kwargs.get('slug'))
return get_object_or_404(Blog, slug=slug, user=self.request.user)
and
def get_object(self, queryset=None):
slug = self.kwargs.get('slug')
return get_object_or_404(Blog, slug=unquote(slug), user=self.request.user)
but nothing…
Again as above,
Did you visually verify what you’re getting as self.kwarg.get('slug')
,
Did you verify what you get after the unquote
function`?
Did you check to verify that that slug exists in the database for the current user?
Since the slug in this case should be unique, do you really need the additional user
attribute in the query?
Debugging something like this involves working this through step-by-step, ensuring that everything is happening as it should at each step of the process.
Using the Django shell is very useful for checking out things like this.
1 Like