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
6
votes
1 answer

ordering in listview of django

i am in django 2 and facing some issues here. Let me give you my files first views.py class home_view(ListView): model = home_blog_model template_name = "home.html" context_object_name = "posts" paginate_by = 6 ordering =…
Nitin Khandagale
  • 413
  • 5
  • 14
6
votes
2 answers

Django CreateView: set user before validation

I have a model that uses different validation for its name field depending on whether the object was created by a user or by the system. class Symbol(models.Model): name = models.CharField(_('name'), unique=True, max_length=64) creator =…
6
votes
3 answers

Django CreateView field labels

I'm working on a project that has a Chapter, with each Chapter having a title, content, and order. I'd like to keep the field 'order' named as is, but have the field displayed in a CreateView as something else, like 'Chapter number'. The best…
brunosardinepi
  • 125
  • 1
  • 7
6
votes
1 answer

How to add extra context & queryset field in Django generic views?

I'm building Django application, with submitting links and voting functionality. I want to show all links, voted by a user in user details page. I can retrieve them in python shell using this: Link.objects.filter(votes__voter=user) But I don't know…
6
votes
2 answers

Override values in Django ModelForm instance during get_form()

I have a model "SavedSearch" with a generic UpdateView. Each site user can have one Saved Search. When running a search on the site, I want to give them the ability to click a "Save this Search" button that will take them to the edit form for…
Psyferre
  • 135
  • 2
  • 9
6
votes
2 answers

How can I tell if I'm doing a create or an update with django generic views (CreateView vs UpdateView) from my template?

I'm sharing the same template for my CreateView and UpdateView using django's generic views. I want the "submit" button in my template to say "Add" when I'm using the CreateView and "Update" when I'm using the UpdateView. Is there any way in my…
mgalgs
  • 15,671
  • 11
  • 61
  • 74
6
votes
3 answers

Django: How to set a hidden field on a generic create view?

I'm running Django 1.6.x To extend my user I've added another model storing the data: class UserProfile (models.Model): user = models.ForeignKey(User) height = models.IntegerField(blank=True, null=True) Now I wand to add a view, which is…
frlan
  • 6,950
  • 3
  • 31
  • 72
6
votes
2 answers

Combining DetailView and CreateView in Django 1.6

I have 2 separate models, Post and Comment. I use DetailView to display Post contents and I want to use a CreateView to display comment creation form on the same page. What is the cleanest way to go about that? The only thing that comes to mind is…
6
votes
2 answers

Manually get response from class-based generic view

I'm trying to write a test that validates HTML returned from a generic class-based view. Let's say I have this function-based view that simply renders a template: # views.py from django.shortcuts import render def simple_view(request,…
6
votes
1 answer

Django: best way to add filtering (and sorting) to (generic) class based ListView?

Let's say I have a model like this: class Car(models.Model): BRANDS = ( ('FRD', 'Ford'), ('MCD', 'Mercedes'), ... ) brand = models.CharField(max_length=3, choices=BRANDS) color =…
Paul Bormans
  • 1,292
  • 16
  • 22
6
votes
1 answer

Django - queryset vs model in Generic View

I'm pretty new at Django and wondering what is the difference between defining model vs queryset in a generic view like ListView. Here's my code example in my urls.py file for the project: urlpatterns = patterns('', url(r'^$',…
5
votes
5 answers

Django: How do I update a model after a view has been rendered?

I have a view that displays a list of models. Some of the properties of some of the models need to be updated after the view has been rendered: that is, the user is expected to see the original, unchanged values on first visit and the updated values…
tawmas
  • 7,443
  • 3
  • 25
  • 24
5
votes
1 answer

What is the difference between mixins and generics?

I'm learning about the Django Rest Framework. And there are two concepts that from my point of view are almost the same, and they are used in different scenarios. rest_framework mixins I think that they are used when we use viewsets. And…
Jacobo
  • 1,259
  • 2
  • 19
  • 43
5
votes
4 answers

Django detailview get_queryset and get_object

I am using Django detailview. initially, I used the URL pattern url(r'^todo/details/(?P[\d]+)', views.todoDetailView.as_view(), name='detail_todo'), my view is class todoDetailView(DetailView): model = models.todo It worked fine. In the…
Sandesh Ghanta
  • 385
  • 2
  • 4
  • 16
5
votes
0 answers

Unable to use DateTime input through django CreateView and ModelForm

I tried to create an object of Model Event through UI using CreateView My Model is class Event(models.Model): start = models.DateTimeField(_("start"), db_index=True) end = models.DateTimeField(_("end"), db_index=True, help_text=_("The end…