Questions tagged [django-generic-views]

Questions about Django’s built-in generic views, which helps you to avoid repeating common code patterns in every project.

Questions about Django’s built-in generic views, which helps you to avoid repeating common code patterns in every project.

568 questions
113
votes
7 answers

Django class-based view: How do I pass additional parameters to the as_view method?

I have a custom class-based view # myapp/views.py from django.views.generic import * class MyView(DetailView): template_name = 'detail.html' model = MyModel def get_object(self, queryset=None): return…
user9903
69
votes
5 answers

Use get_queryset() method or set queryset variable?

These two pieces of code are identical at the first blush: class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_poll_list' queryset = Poll.active.order_by('-pub_date')[:5] and class…
9Algorithm
  • 1,258
  • 2
  • 16
  • 22
35
votes
3 answers

Accessing request.user in class based generic view CreateView in order to set FK field in Django

So I have a model that includes: class Place(models.Model): .... created_by = models.ForeignKey(User) My view is like so: class PlaceFormView(CreateView): form_class = PlaceForm @method_decorator(login_required) def…
Brian
  • 931
  • 2
  • 9
  • 13
33
votes
3 answers

Redirecting after AJAX post in Django

I use Django's built-in DeleteView and I've assigned a value to the success_url attribute. Now in my template, I trigger this view via JQuery' $.post() method. When the item is deleted, I don't get redirected to the success_url. After some search, I…
Xiao Liang
  • 763
  • 2
  • 7
  • 12
32
votes
2 answers

Expected view to be called with a URL keyword argument named "pk"

I'm writing a test for a Django Rest Framework view following closely the testing documentation Here's my simple test: def test_patient_detail_api_opens(self): factory = APIRequestFactory() view =PatientDetailApi.as_view() request =…
27
votes
1 answer

Curious about get_form_kwargs in FormView

I was having trouble with FormView recently and found that the way to go about doing it was to use get_form_kwargs. Here is my code: class InternalResetPasswordView(FormView): template_name = 'reset_password.html' form_class =…
26
votes
19 answers

Bootstrap btn-block not working

I'm trying to expand the submit button so that it is the size of the password field. I am using the code btn-block but it's not working.
user3553966
  • 319
  • 1
  • 3
  • 7
24
votes
5 answers

how overwrite Response class in django rest framework ( DRF )?

I want to overwrite Response class of django rest framework so that response back responsive dictionary contain three parameter message, status and data Hello dears all I try to change Response Class in DRF to pass two extra parameter ( message,…
24
votes
3 answers

In plain English, what are Django generic views?

The first two paragraphs of this page explain that generic views are supposed to make my life easier, less monotonous, and make me more attractive to women (I made up that last one): https://docs.djangoproject.com/en/1.4/topics/generic-views/ I'm…
allyourcode
  • 21,871
  • 18
  • 78
  • 106
23
votes
2 answers

Manually calling a class based generic view in Django

I'm currently trying to call a class based Generic view from within another class based generic view and cant seem to do it correctly. Ways I've tried: result = CategoryTypes.as_view() # The same way you put it in the urlconf print result Prints:…
Joshua Jonah
  • 278
  • 1
  • 2
  • 6
19
votes
3 answers

Python multiple inheritance function overriding and ListView in django

I created a class that subclasses ListView and two custom mixins which have implemented a get_context_data function. I wanted to override this function on the child class: from django.views.generic import ListView class ListSortedMixin(object): …
19
votes
2 answers

How does one use a custom widget with a generic UpdateView without having to redefine the entire form?

I have a model with a ManyToMany relation that I would like to update with a CheckBoxSelectMultiple widget while everything else uses the default generic form, but when I redefine that one form field, it is the only one that shows up in the…
skzryzg
  • 1,020
  • 8
  • 25
18
votes
4 answers

Django Generic Views: When to use ListView vs. DetailView

I am using Django's class based generic views in a blog application. One of my views displays a list of posts that have a certain tag. I can write this view as a ListView of posts, filtered by tag. Or I can write this view as a DetailView of the…
user1272534
  • 991
  • 2
  • 11
  • 17
18
votes
4 answers

how to send success message if we use django generic views

I am new to django (1.2.4). I have created some crud with generic views. But How can I show something like "The student was added successfully" when student is created using django's messaging framework?
Myth
  • 1,562
  • 5
  • 15
  • 25
18
votes
1 answer

Extending generic view classes for common get_context_data

I constantly see myself having to add the same extra variable to the context of many of my views. def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(MyListView,…
Clash
  • 4,896
  • 11
  • 47
  • 67
1
2 3
37 38