How can i pass the context data which is coming from a forms.py to my views class which is using generic detailView, i need to pass forms.py to my product detail page.
Here is the code for my view class
class ProductView(generic.DetailView):
model = Product
cart_product_form = CartAddProductForm()
context = {'cart_product_form': cart_product_form}
template_name = 'shopping/product.html'
query_pk_and_slug = True
Please let me know if this is incorrect
Override get_context_data, and add the form to the context before returning it.
class ProductView(generic.DetailView):
model = Product
template_name = 'shopping/product.html'
query_pk_and_slug = True
def get_context_data(self, **kwargs):
context = super(ProductView, self).get_context_data(**kwargs)
cart_product_form = CartAddProductForm()
context['cart_product_form'] = cart_product_form
return context
Related
I have this class that changes user password:
class PasswordsChangeView(PasswordChangeView):
from_class = PasswordChangingForm
success_url = reverse_lazy(
"base:password/passwordsuccess",
)
And I would like to insert User Avatar Img Path in context.
This Img is inside a UserComplement model.
When I create function views, it works doing:
#login_required
def password_success(request):
try:
obg = UserComplement.objects.get(pk=request.user.id)
context = {
"img_avatar": obg.avatar_image,
}
return render(request, "registration/passwordsuccess.html", context)
except UserComplement.DoesNotExist:
return render(request, "registration/passwordsuccess.html")
The problem is that I cannot get the userID in classviews to pass it through the context on success_url.
It was a lack of knowledge on how class based views work. The function get_context_data did the job well, returning the context.
class PasswordsChangeView(PasswordChangeView):
from_class = PasswordChangingForm
success_url = reverse_lazy(
"base:password/passwordsuccess",
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
obj = UserComplement.objects.get(pk=self.request.user.id)
context["img_avatar"] = obj.avatar_image
return context
Consider the these two models and view:
models.py
class BHA_List(models.Model):
well = models.ForeignKey(WellInfo, 'CASCADE', related_name='bha_list')
bha_number = models.CharField(max_length=100)
class BHA_overall(models.Model):
bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_overall')
drill_str_name = models.CharField(max_length=111)
depth_in = models.CharField(max_length=111)
views.py
class BHA_UpdateView(UpdateView):
model = BHA_overall
pk_url_kwarg = 'pk_alt'
form_class = BHA_overall_Form
To my understanding, pk_url_kwarg = 'pk_alt' will query and return instances of model = BHA_overall.
Let's say that I use a different CBV other than UpdateView, and want to implement two models. So something like this:
model = (BHA_overall, BHA_List). Is there any way that I force my pk_url_kwarg = 'pk_alt' to query and return instances only in BHA_List, but force my get_object() return objects in BHA_overall?? What CBV should I use?
you can use just 'View' and define methods post and get.
about like this:
class SomeView(View):
model = BHA_List
template_name = 'some.html'
def get(request, **kwargs):
overall = BHA_overall.objects.all()
return render(request,
self.template_name,
locals())
def get(request, **kwargs):
return render(request, self.template_name, {})
I try to use class based views in Django. And I have such problem: I define a base class for a blog (BlogBaseView) and two other classes, that inherit it.
And in the second class(BlogIndexView) I want to make the search by get request, so I have override get method. It works, but if I don't make get request, it returns HttpResponse, however I want to return usual context (which BlogIndexView retunes without override get method).
What can I do?
class BlogBaseView(View):
def get_context_data(self, **kwargs):
context = super(BlogBaseView, self).get_context_data(**kwargs)
blog_categories = []
categories = BlogCategory.objects.all()
for category in categories:
blog_categories.append(tuple([category, category.get_number_of_category_items]))
context['name_page'] = 'blog'
context['tags'] = Tag.objects.all()
context['blog_categories'] = blog_categories
return context
class BlogIndexView(BlogBaseView, ListView):
queryset = Post.objects.all().order_by('-date_create')
template_name = 'index_blog.html'
context_object_name = 'posts'
def get(self, request):
if request.GET.get('tag'):
context = {
'posts' : Post.objects.filter(tags__name__in=[request.GET.get('tag')])
}
return render(request, self.template_name, context)
return HttpResponse('result')
class BlogFullPostView(BlogBaseView, DetailView):
model = Post
template_name = 'full_post.html'
pk_url_kwarg = 'post_id'
context_object_name = 'post'
Thanks!
ListView class also has a get_context_data method, so you should override that instead of get method. Using super you'll get access to BlogBaseView.get_context_data and then you can extended the result.
Here's how:
class BlogIndexView(BlogBaseView, ListView):
queryset = Post.objects.all().order_by('-date_create')
template_name = 'index_blog.html'
context_object_name = 'posts'
def get_context_data(self, **kwargs):
# use the (super) force Luke
context = super(BlogIndexView, self).get_context_data(**kwargs)
if self.request.GET.get('tag'):
context['posts'] = Post.objects.filter(tags__name__in=[self.request.GET.get('tag')])
return context
If you are overriding ListView then it's not a good idea to override the get method, as you will lose a lot of the ListView functionality.
In this case, it would be a better idea to override get_queryset, and do the search there.
def get_queryset(self):
queryset = super(BlogIndexView, self). get_queryset()
if request.GET.get('tag'):
queryset = queryset.filter(tags__name=request.GET['tag'])
return queryset
How do I write a DetailView in Django, that has other related Queries?
Here is my example:
class DistributionDetailView(DetailView):
model = Distribution
template_name = 'projects/view_distribution.html'
def get_context_data(self, **kwargs):
context = super(DistributionDetailView,
self).get_context_data(**kwargs)
context['weekly_attendance'] = WeeklyAttendance.objects.filter(
distibution_name=Distribution)
context['sales_data'] = SalesData.objects.filter(
distibution_name=Distribution)
context['theatrical_release'] = TheatricalRelease.objects.filter(
distibution_name=Distribution)
return context
Distribution is the model class, you can't use it inside filter(). In the get_context_data method, you can access the current object with self.object, for example:
context['weekly_attendance'] = WeeklyAttendance.objects.filter(
distibution_name=self.object)
You can access the object with self.object in most generic views:
class DistributionDetailView(DetailView):
model = Distribution
template_name = 'projects/view_distribution.html'
def get_context_data(self, **kwargs):
context = super(DistributionDetailView,
self).get_context_data(**kwargs)
context['weekly_attendance'] = WeeklyAttendance.objects.filter(
distibution=self.object)
context['sales_data'] = SalesData.objects.filter(
distibution=self.object)
context['theatrical_release'] = TheatricalRelease.objects.filter(
distibution=self.object)
return context
I'm trying to use request.session to create a 'recent' session key and add the product pages visited by the user to make it accesible in the template, here is my view, what would you guys recommend, I can't seem to go about doing this
class ProductDetail(DetailView):
model = Producto
template_name = 'productos/product_detail.html'
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(ProductDetail, self).get_context_data(**kwargs)
# Add in a QuerySet of featured products
context['product_list'] = Producto.objects.filter(featured=True).exclude(pk=self.object.pk)
return context
Thanks for your help!
thanks to Daniel Roseman for the clarification on how to call session from the class based generic view
class ProductDetail(DetailView):
model = Producto
template_name = 'productos/product_detail.html'
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(ProductDetail, self).get_context_data(**kwargs)
if not 'recent' in self.request.session or not self.request.session['recent']:
self.request.session['recent'] = [self.object.pk]
else:
recentList = self.request.session['recent']
recentList.append(self.object.pk)
self.request.session['recent'] = recentList
# Add in a QuerySet of featured products
context['product_list'] = Producto.objects.filter(featured=True).exclude(pk=self.object.pk)
context['recent_list'] = Producto.objects.filter(pk__in=recentList)
return context