53
I think your goal is trying to filter queryset based on form submission, if so, by using GET :
class ProfileSearchView(ListView)
template_name = '/your/template.html'
model = Person
def get_queryset(self):
name = self.kwargs.get('name', '')
object_list = self.model.objects.all()
if name:
object_list = object_list.filter(name__icontains=name)
return object_list
Then all you need to do is write a get
method to render template and context.
Maybe not the best approach. By using the code above, you no need define a Django form.
Here’s how it works : Class based views separates its way to render template, to process form and so on. Like, get
handles GET response, post
handles POST response, get_queryset
and get_object
is self explanatory, and so on. The easy way to know what’s method available, fire up a shell and type :
from django.views.generic import ListView
if you want to know about ListView
and then type dir(ListView)
. There you can see all the method defined and go visit the source code to understand it. The get_queryset
method used to get a queryset. Why not just define it like this, it works too :
class FooView(ListView):
template_name = 'foo.html'
queryset = Photo.objects.all() # or anything
We can do it like above, but we can’t do dynamic filtering by using that approach. By using get_queryset
we can do dynamic filtering, using any data/value/information we have, it means we also can use name
parameter that is sent by GET
, and it’s available on kwargs
, or in this case, on self.kwargs["some_key"]
where some_key
is any parameter you specified
16
Well, I think that leaving validation to form is nice idea. Maybe not worth it in this particular case, because it is very simple form – but for sure with more complicated one (and maybe yours will grow also), so I would do something like:
class ProfileList(ListView):
model = Profile
form_class = ProfileSearchForm
context_object_name = 'profiles'
template_name = 'pages/profile/list_profiles.html'
profiles = []
def get_queryset(self):
form = self.form_class(self.request.GET)
if form.is_valid():
return Profile.objects.filter(name__icontains=form.cleaned_data['name'])
return Profile.objects.all()
- [Django]-XlsxWriter object save as http response to create download in Django
- [Django]-Django: get table name of a model in the model manager?
- [Django]-Making a Django form class with a dynamic number of fields
16
This is similar to @jasisz ‘s approach, but simpler.
class ProfileList(ListView):
template_name = 'your_template.html'
model = Profile
def get_queryset(self):
query = self.request.GET.get('q')
if query:
object_list = self.model.objects.filter(name__icontains=query)
else:
object_list = self.model.objects.none()
return object_list
Then all you have to do on the html template is:
<form method='GET'>
<input type='text' name='q' value='{{ request.GET.q }}'>
<input class="button" type='submit' value="Search Profile">
</form>
- [Django]-Where should utility functions live in Django?
- [Django]-How do you use get_context_data with TemplateView in Django
- [Django]-Populating django field with pre_save()?
4
This has been explained nicely on the generic views topic here Dynamic filtering.
You can do filtering through GET
, I don’t think you can use POST
method for this as ListView
is not inherited from edit mixings.
What you can do is:
urls.py
urlpatterns = patterns('',
(r'^search/(\w+)/$', ProfileSearchListView.as_view()),
)
views.py
class ProfileSearchListView(ListView):
model = Profile
context_object_name = 'profiles'
template_name = 'pages/profile/list_profiles.html'
profiles = []
def get_queryset(self):
if len(self.args) > 0:
return Profile.objects.filter(name__icontains=self.args[0])
else:
return Profile.objects.filter()
- [Django]-Serializer call is showing an TypeError: Object of type 'ListSerializer' is not JSON serializable?
- [Django]-Django internationalization for admin pages – translate model name and attributes
- [Django]-Django formsets: make first required?
1
I think that the error you are getting is because your form doesn’t require the name field. So, although the form is valid, the cleaned_data for your name
field is empty.
These could be the problematic lines:
if form.is_valid():
self.show_results = True
self.profiles = Profile.objects.filter(name__icontains=form.cleaned_data['name'])
If I were you, I would try changing the line:
self.profiles = Profile.objects.filter(name__icontains=form.cleaned_data['name'])
to this:
self.profiles = Profile.objects.none()
If you stop receiving errors (and your template receives an empty object_list
), the problem you have is what I said before: name field not required.
Let us know if this doesn’t work!
- [Django]-Is there Django List View model sort?
- [Django]-Trying to migrate in Django 1.9 — strange SQL error "django.db.utils.OperationalError: near ")": syntax error"
- [Django]-Images from ImageField in Django don't load in template
1
Search on all fields in model
class SearchListView(ItemsListView):
# Display a Model List page filtered by the search query.
def get_queryset(self):
fields = [m.name for m in super(SearchListView, self).model._meta.fields]
result = super(SearchListView, self).get_queryset()
query = self.request.GET.get('q')
if query:
result = result.filter(
reduce(lambda x, y: x | Q(**{"{}__icontains".format(y): query}), fields, Q())
)
return result
- [Django]-Generate a Unique String in Python/Django
- [Django]-The view didn't return an HttpResponse object. It returned None instead
- [Django]-"Unknown command syncdb" running "python manage.py syncdb"
0
def get_queryset(self):
query_name = self.request.GET.get('query', '')
object_list = Product.objects.filter(
Q(title__icontains=query_name)
)
return object_list
<form action="" method="GET">
{% csrf_token %}
<input type="text" name="query" placeholder="Search keyword">
<i class="ti-search"></i>
</form>
- [Django]-How can I set a default value for a field in a Django model?
- [Django]-Datetime and timezone conversion with pytz – mind blowing behaviour
- [Django]-Different db for testing in Django?